首页 文章

调整TableView中的单元格大小

提问于
浏览
-1

我试图让我的一些细胞更高 . 我试过用

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 *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

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

谢谢您的帮助

3 回答

  • 2

    使用 tableView:heightForRowAtIndexPath 方法 .

    Apple文档清楚地解释了该做什么 . UITableView class reference

    每个tableView都有一个委托属性 . 将它设置到viewController并实现上面的方法 . 它的签名是

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

    因此,根据 indexPath ,返回您想要的任何高度 .

    如果要为所有行保持恒定高度,可以使用 UITableViewrowHeight 属性 .

  • 0

    使用UITableViewDelegate的 heightForRowAtIndexPath . 例:

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

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

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

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

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

相关问题