学习自定义UITableViewHeaderFooterView的几点总结:
1.自定义的HeaderFooterView一般是用在列表的头部标题,显示不同的状态信息。系统自带的HeaderFooterView不能很好地达到显示效果。如果只需要显示文字,可以调用:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section2.如果同时调用
<pre name="code" class="objc"><span style="font-size:18px;">-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section</span>
和
<span style="font-size:18px;">-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section</span>则系统会默认调用-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section这个方法,返回一个字符串。
3.在自定义的HeaderFooterView初始化的时候只需要确定需要添加的各子控件,无需赋值数据。
设置各子控件的Frame的时候调用
-(void)layoutSubviews方法来设置各个子控件的Frame。
注意一定要先调用[super layoutSubviews],否则会出现各种奇葩问题。切记一定要调用。
4.
* 当一个控件被添加到父控件中就会调用
1> - (void)didMoveToSuperview;
* 当一个控件即将被添加到父控件中会调用
2> - (void)willMoveToSuperview:(UIView *)newSuperview;
两者选择一种方法实现即可,HeaderFooterView会在TableView滚动过程被销毁,而不是像UITableViewCell那样会从缓存池循环利用,在调用
[self.tableViewreloadData]刷新数据的时候,会创建新的HeaderFooterView,如果需要改变子控件的属性则需要调用以上方法。
5.以下是设置HeaderFooterView中图片的几种模式,很有实用价值。
friendsView.imageView.contentMode=UIViewContentModeCenter;
friendsView.imageView.clipsToBounds=NO或friendsView.imageView.layer.masksToBounds=NO;

本文详细总结了自定义UITableViewHeaderFooterView的方法、使用场景及注意事项,包括文字显示、图片设置、布局管理、控件生命周期回调等关键点。
1万+

被折叠的 条评论
为什么被折叠?



