关于UITableViewCell重用的问题

jianghuanjianghuan 2015-05-09 10:49:14
cell高度不固定,下面的写法cell重用的时候就有问题:

/**
*TestCell.h
*/
@interface TestCell : UITableViewCell
//cell高度由自己的内容决定
@property(nonatomic,assign) CGFloat *cellHeight;
@property(nonatomic,weak) UILabel *label;
@end

/**
*TestCell.m
*/
@implementation TestCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
NSLog(@"初始化cell");
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self != nil) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
_label = label;
[self addSubview:label];
}
return self;
}
@end

/**
*TestTableCtr.m
*/
@interface TestTableCtr ()
@property(nonatomic,strong)NSMutableArray *tableData;
@end

@implementation TestTableCtr
-(NSMutableArray *)tableData{
if (_tableData == nil) {
_tableData = [NSMutableArray array];
for (int i=0; i<50; i++) {
[_tableData addObject:[NSString stringWithFormat:@"第%d行",i]];
}
}
return _tableData;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//方式1:先取到cell,再根据cell的高度返回对应的高度,但是这种方式cell的重用就有问题,cell会初始化很多次
TestCell *cell = (TestCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
//方式2:直接返回定死的高度,这样cell重用没有问题,但不能动态返回高度
//return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"生成一个cell");
static NSString *identifier = @"identifier";
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.label.text = _tableData[indexPath.row];
cell.bounds = CGRectMake(0, 0, cell.bounds.size.width, 20 + indexPath.row);
return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.tableData.count;
}

@end


求大神们指点。
...全文
193 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
不担心 2015-05-17
  • 打赏
  • 举报
回复
如果是用autolayout布局的话确实是通过渲染出cell再返回这个cell的高度的 但是不是你这种简单粗暴的做法,还有很多细节要实现,这里有一篇教程说的很详细,你看了就明白了 http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan-uitableviewcellgao-du-xiang-jie/
jianghuanjianghuan 2015-05-10
  • 打赏
  • 举报
回复
引用 1 楼 disburden 的回复:
不能这样计算cell的高度,你要根据你cell中会变化高度的控件来计算出cell的高度 目测了一下你这个cell会导致高度变化的应该_tableData[indexPath.row];这个字符串 所以你应该用 NSString *str=_tableData[indexPath.row]; CGsize size=[str boundingRectWithSize....]; 这个方法计算出字符串的高度,然后再加上其他固定尺寸视图的高度 这个方法比较通用,ios8后有另外一种办法,网络上有教程,这里就不说了
嗯,,这种方法可以,,但是我想知道我这样写为什么cell重用就有问题了,,我发现只要在-tableView:heightForRowAtIndexPath:里面调用了-tableView:cellForRowAtIndexPath:方法cell重用就有问题,但是如果我用xib文件,autolayout布局动态高度的cell,就需在-tableView:heightForRowAtIndexPath:里面调用了-tableView:cellForRowAtIndexPath:,如下:

///Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   //取得cell
    AppointmentHisCell *cell = (AppointmentHisCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
   //计算高度 
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height;
}
我看网上的文章用xib定义cell然后自动布局就是这样写的,但是我就发现在-tableView:heightForRowAtIndexPath:里面用-tableView:cellForRowAtIndexPath:这个方法,cell重用就有问题,如果不能这样调用,那用xib定义cell,用自动布局又该怎样计算高度呢?
Bannings 2015-05-09
  • 打赏
  • 举报
回复
在 init 中只做 addSubview 操作,然后在 layoutSubviews 方法里做布局,每调用一次 layoutSubviews 方法,就计算布局一次
不担心 2015-05-09
  • 打赏
  • 举报
回复
不能这样计算cell的高度,你要根据你cell中会变化高度的控件来计算出cell的高度 目测了一下你这个cell会导致高度变化的应该_tableData[indexPath.row];这个字符串 所以你应该用 NSString *str=_tableData[indexPath.row]; CGsize size=[str boundingRectWithSize....]; 这个方法计算出字符串的高度,然后再加上其他固定尺寸视图的高度 这个方法比较通用,ios8后有另外一种办法,网络上有教程,这里就不说了

29,041

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧