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

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

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

因为考虑不周,在tableView中没有使用xib,因此初始化用的上面的方法。code

傻眼了,若是在 heightForRowAtIndexPath 中返回的高度大于cell自己的height,cell中的imageView按照cell的frame调整,cell调整不到位,下方空白。blog

若是heightForRowAtIndexPath 中返回的高度小于cell自己的height,tableView中cell发生折叠。继承

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

======================================================table

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

-(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执行的。im

相关文章
相关标签/搜索