如何处理具有不同单元格高度的UITableView?

发布于 2024-11-26 14:38:15 字数 217 浏览 1 评论 0原文

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

谢谢!

each cell in my table has a label, with maximum three lines, so the cell height depends on the label's height, I know the delegate method that returns height for a specific cell(NSIndexPath), but I need to get the actual cell in order to determine the height, and if I call cellForRow:atIndexPath: in heightForCell:, it loops infinitely, I guess cellForRow also calls heightForCell. So is there any way I can handle this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余罪 2024-12-03 14:38:15

您可以在 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

蒂姆

You can use NSStrings size for string in the heightForRow: delegate method e.g.

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];

This will give you a CGSize you can then use the height from here and add a little padding. You can find more information here: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

Tim

面如桃花 2024-12-03 14:38:15

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

Use – sizeWithFont:constrainedToSize:lineBreakMode: of NSString (you can find more on this on this portal) or use an array containing height for every indexPath as shown in this example (SectionInfo class) http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文