Gre*_*reg 27 iphone uitableview ios heightforrowatindexpath
怎样才能获得UITableViewCell当内heightForRowAtIndexPath方法,即给予indexPath?
(然后我可以访问我创建的内容视图以增加其高度)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// How to get the UITableViewCell associated with this indexPath?
}
Run Code Online (Sandbox Code Playgroud)
谢谢
编辑:事实上,真的有一个有效的方法来做到这一点?当我发表一些NSLog陈述时,它似乎heightForRowAtIndexPath在调用之前调用了几次cellForRowAtIndexPath(这是我UILabels在单元格中设置的位置)?这种意味着我可能会尝试使用一种不起作用的技术,即我希望heightForRowAtIndexPath能够访问每个单元格中已经创建的标签以获得它们的高度并将它们加在一起以获得整个单元格行高度,但是如果它们没有它们的话尚未设置(内cellForRowAtIndexPath)然后我想我的方法不能真正起作用?
ios*_*ist 24
您可以使用tableview的委托而不是tableView本身.
id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
在这里查看答案
在新的iOS版本中并使用正确的自动约束,您不再需要引用单元格来计算单元格的高度.
点击这里
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
haa*_*awa 19
这个问题毫无意义,因为在
heightForRowAtIndexPath
Run Code Online (Sandbox Code Playgroud)
尚未创建单元格.这是tableView的工作原理:
-numberOfSectionsInTableView-numberOfRowsInSection- heightForRowAtIndexPath-cellForRowAtIndexPath因此,您无法访问单元格,heightForRowAtIndexPath因为尚未创建最可能的单元格.
但是,如果我误解了你的问题,请尝试:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
sku*_*kue 14
显而易见的答案是打电话cellForRowAtIndexPath,但您可能已经发现存在一些问题.如果你还没有看到这个问题:UITableView flexible/dynamic heightForRowAtIndexPath
对于我的上一个项目,我使用了UICell的自定义子类并实现了这样的方法.然后我调用了这个table:heightForRowAtIndexPath:(在查找该行的内容之后).
+ (CGFloat) heightOfContent: (NSString *)content
{
CGFloat contentHeight =
[content sizeWithFont: DefaultContentLabelFont
constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
lineBreakMode: UILineBreakModeWordWrap].height;
return contentHeight + DefaultCellPaddingHeight;
}
Run Code Online (Sandbox Code Playgroud)
Kin*_*iss -19
对于iOS8:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
或者
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
但对于iOS7来说,关键是计算autolayout后的高度,
func calculateHeightForConfiguredSizingCell(cell: GSTableViewCell) -> CGFloat {
cell.setNeedsLayout()
cell.layoutIfNeeded()
let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0
return height
}
Run Code Online (Sandbox Code Playgroud)
笔记:
1) 如果有多行标签,不要忘记将 设为numberOfLines0。
2)不要忘记label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)
| 归档时间: |
|
| 查看次数: |
30903 次 |
| 最近记录: |