繁体   English   中英

在TableView中调整单元格的大小

[英]Resizing a cell in a TableView

我试图使我的一些单元格更高。 我尝试使用

CGRect rect = cell.frame;
NSLog(@"before height: %f",rect.size.height);
rect.size.height +=20;

cell.frame = rect;
NSLog(@"AFTER height: %f",cell.frame.size.height);

cellForRowAtIndexPath

willDisplayCell:(UITableViewCell *)RowAtIndexPath:(NSIndexPath *)indexPath单元格

日志显示值已更改,但模拟器中未显示任何更改。

谢谢您的帮助

使用tableView:heightForRowAtIndexPath方法。

苹果文档清楚地说明了该怎么做。 UITableView类参考

每个tableView都有一个委托属性,将其设置为您的viewController并实现上述方法。 它的签名是

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

因此,基于indexPath ,返回您想要的任何高度。

如果您希望所有行的高度都恒定,则可以使用UITableView rowHeight属性。

使用UITableViewDelegate的heightForRowAtIndexPath 例:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row == _pages.count - 1 ? 408 : 450;
}

为了使某些单元格更大,您应该实现tableView:heightForRowAtIndexPath:方法tableView:heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (<SOMETHING>) {
        return height1;
    } else {
        return height2;
    }
}

tableView:cellForRowAtIndexPath:用于配置tableView需要显示的单元格的内容。

请注意,如果您有一个非常大的表(1000多个条目),则tableView:heightForRowAtIndexPath:会对性能产生影响,在显示表视图时,每行都会调用此方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM