【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

相關文章
相關標籤/搜索