【iOS问题记录】关于UITableViewCell的高度、填充

创建了继承自UITableViewCell的类,在创建该类的同时创建了.xib文件,在cell中填充UIImageView,其frame根据cell的frame调整。在.m中添加以下方法:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        [self configUI];
    }
    return self;
}

由于考虑不周,在tableView中没有使用xib,所以初始化用的上面的方法。

傻眼了,如果在 heightForRowAtIndexPath 中返回的高度大于cell本身的height,cell中的imageView按照cell的frame调整,cell调整不到位,下方空白。

如果heightForRowAtIndexPath 中返回的高度小于cell本身的height,tableView中cell发生折叠。

概念还不是很清楚,不明白为什么这么做。暂时的解决方案是设置cell的self.frame.size.height与heightForRowAtIndexPath中返回值一致。

======================================================

发现自己2了,想到的一种比较简单的方法就是:填充玩cell后,set该cell的frame,在heightForRowAtIndexPath中得到该cell,返回cell.frame.size.height。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return 80;
    GogoTableViewCell *cell = (GogoTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

千万别用 [tableView cellForRowAtIndexPath:indexPath];在heightForRowAtIndexPath是先于

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath执行的。

文章版权归个人所有,转载时请在文章显眼位置给出本文链接。
原文地址:https://www.cnblogs.com/xjshi/p/4260654.html