【问题标题】:How to handle UITableView with different cell height?如何处理具有不同单元格高度的 UITableView?
【发布时间】:2013-06-06 11:13:48
【问题描述】:

表格中的每个单元格都有一个标签,最多三行,因此单元格高度取决于标签的高度,我知道返回特定单元格高度的委托方法(NSIndexPath),但我需要获取实际的cell 来确定高度,如果我在 heightForCell: 中调用 cellForRow:atIndexPath:,它会无限循环,我猜 cellForRow 也调用 heightForCell。那么有什么办法可以解决这个问题吗?

谢谢!

【问题讨论】:

    标签: uitableview delegates height


    【解决方案1】:

    您可以在 heightForRow: 委托方法中使用 NSStrings 大小作为字符串,例如

    CGSize maxSize = CGSizeMake(300, 800); //max x width and y height
    NSString *cellTitle = @"Lorem ipsum";
    UIFont *stringFont = [UIFont systemFontOfSize:14]; // use the same font your using on the cell
    CGSize cellStringSize = [myString sizeWithFont:stringFont constrainedToSize:maximumSize lineBreakMode: UILineBreakModeWordWrap];
    

    这将为您提供 CGSize,然后您可以使用此处的高度并添加一点填充。你可以在这里找到更多信息:http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

    提姆

    【讨论】:

      【解决方案2】:

      使用– sizeWithFont:constrainedToSize:lineBreakMode:NSString(您可以在此门户上找到更多信息)或使用包含每个indexPath 的高度的数组,如本示例所示(SectionInfo 类)http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        相关资源
        最近更新 更多