iOS Developer

关于UITableView的HeaderView循环的问题

2017-02-28  本文已影响581人  幸福的尾巴__
最近写了一个UITableView内即有section又有row的小项目,顺便在row内添加btn来从新刷新这个UITableView的数据源.
一直到写完加载 显示ui都没有发现有什么问题,包括内存方面,可是在添加点击事件后,用block回调来刷新UITableView的时候发现有bug了,滚动的时候点击会反复的跳动 UITableViewCell 搞得点击btn后 页面闪动很大.
初步猜测有以下一些原因:

关于- tableView:viewForHeaderInSection:
一般这个方法- tableView:viewForHeaderInSection:我会这么调用

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    //设置数据源 xxxx.....
    return view;
}

突然感觉这个方法应该向cell那样 有循环的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
 //设置数据源 xxxx.....
    return cell;
}

在实例化cell的时候一般都会实现 -dequeueReusableCellWithIdentifier:forIndexPath:方法来达到循环使用cell的目的.
所以看了相关apple的API发现确实有一个方法 来设置这个UITableViewHeader的,是在iOS6引入的一个类UITableViewHeaderFooterView你需要使用 -initWithReuseIdentifier:方法来使HeaderView达到循环的目的

从写此方法后run发现HeaderView竟然木有了...木有了....-_-!

一定是哪里出了问题,谷歌搜索后说需要在实例化的 HeaderView内种重写 ``` - (instancetype)initWithReuseIdentifier:reuseIdentifier


重写```- (UIView *)tableView:viewForHeaderInSection:```方法

pragma mark 返回每组的头部视图

在写完后 ```run```有报警告 ``` UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead ``` 需要从新设置HeaderView的头部颜色要实现```contentView```的方法,直接在xib内修改颜色不起作用哈,把xib的颜色设置成默认,这个警告就消失了,这个是个坑.不过,在xib内在添加一个view改变view颜色也可用达到改变背景颜色的目的.

回归正题,可悲的事情点击btn刷新UITableViewCell后,cell依然乱跳,
重写 ``` - tableView:heightForHeaderInSection:``` 方法....好吧,跟这个方法没关系,略过一些废话.
 
就最后一个了 ```- (CGFloat)tableView:estimatedHeightForHeaderInSection:```
这个方法是iOS7出来的,问题确实出现在这里,在点击cell内的btn调用刷新的时候 高度应该是已经算好了的,不应该在预估一个定值的高度,所以在block回调内当点击btn后 这个方法应该返回真实的高度,而不是预估的高度.
注意:```self.estimatedHeightForRow```是我定义的属性,在cell的block回调内设置成YES;

//根据str的内容计算HeaderView的高度
-(float)autoCalculateWidthOrHeight:(float)height
width:(float)width
fontsize:(float)fontsize
content:(NSString*)content
{
//计算出rect
CGRect rect = [content boundingRectWithSize:CGSizeMake(width, height)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]} context:nil];

//判断计算的是宽还是高
if (height == MAXFLOAT) {
    return rect.size.height;
}
else{
    return rect.size.width;
}

}

补充:
有得时候,你会感觉 这个根据文字算高度的方法 ```-boundingRectWithSize: options:attributes: context:```这个方法算的不准,有那么几个原因
1. 参数 ```options: ```不是```NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading```
2. attributes:参数错误
3. 在xib内设置的样式不是```System```样式的

![xib_lable_Image.png](https://img.haomeiwen.com/i1447422/b9876f9529ebe8a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

具体的代码还在整理中.......


上一篇 下一篇

猜你喜欢

热点阅读