ios 如何在heightForRowAtIndexPath内获取UITableViewCell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5254723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to obtain the UITableViewCell within heightForRowAtIndexPath?
提问by Greg
How does one obtain the UITableViewCell
when within the heightForRowAtIndexPath
method, i.e. given the indexPath?
如何获得方法UITableViewCell
内的时间heightForRowAtIndexPath
,即给定的 indexPath?
(then I could access the content views I have created to add their heights up)
(然后我可以访问我创建的内容视图以增加它们的高度)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// How to get the UITableViewCell associated with this indexPath?
}
thanks
谢谢
EDIT: In fact is there really a valid way to do this? When I put some NSLog
statements it seems that heightForRowAtIndexPath
it called several times before the calls to cellForRowAtIndexPath
(which is where I set up the UILabels
in the cell)? This kind implies that I may be tried to use a technique that will not work, i.e. I was hoping in heightForRowAtIndexPath
to access the already created labels in each cell to get their heights and add them together for the overall cell row height, HOWEVER if they haven't been set up yet (within cellForRowAtIndexPath
) then I guess my approach can't really work?
编辑:事实上,真的有一种有效的方法可以做到这一点吗?当我发表一些NSLog
声明时,它似乎heightForRowAtIndexPath
在调用之前调用了几次cellForRowAtIndexPath
(这是我UILabels
在单元格中设置的位置)?这种意味着我可能会尝试使用一种不起作用的技术,即我希望heightForRowAtIndexPath
访问每个单元格中已经创建的标签以获取它们的高度并将它们加在一起以获得整体单元格行高,但是如果它们没有尚未设置(在 内cellForRowAtIndexPath
)那么我想我的方法真的行不通?
采纳答案by KingofBliss
For iOS8:
对于iOS8:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
OR
或者
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
But for iOS7, the key is calculate the height after autolayout,
但对于iOS7,关键是计算自动布局后的高度,
func calculateHeightForConfiguredSizingCell(cell: GSTableViewCell) -> CGFloat {
cell.setNeedsLayout()
cell.layoutIfNeeded()
let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0
return height
}
Note:
笔记:
1) If multiple lines labels, don't forget set the numberOfLines
to 0.
1)如果有多行标签,不要忘记将其设置numberOfLines
为 0。
2) Don't forget label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)
2)不要忘记 label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)
回答by iosMentalist
You can use the tableview's delegate instead of the tableView itself.
您可以使用 tableview 的委托而不是 tableView 本身。
id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
Check the answer here
检查这里的答案
UPDATED
更新
In new iOS versions and using correct auto constraints you don't need to reference the cell anymore to calculate the cell's height.
在新的 iOS 版本中并使用正确的自动约束,您不再需要引用单元格来计算单元格的高度。
Check here
在这里查看
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
回答by haawa
This question has no sense because in
这个问题没有意义,因为在
heightForRowAtIndexPath
no cells are created yet. Here's how tableView works:
尚未创建任何单元格。下面是 tableView 的工作原理:
- TableView asks it's datasource how many sections it will have.
-numberOfSectionsInTableView
- Asks it's datasource how many rows each section will have (to know how big scroller should be etc.)
-numberOfRowsInSection
- Asks it's delegate height of each visible row. To know where cells will be located.
- heightForRowAtIndexPath
- Lastly it asks datasource to give it a cell to display at given index path
-cellForRowAtIndexPath
- TableView 询问它的数据源它将有多少个部分。
-numberOfSectionsInTableView
- 询问它的数据源每个部分将有多少行(知道滚动条应该有多大等)
-numberOfRowsInSection
- 询问每个可见行的委托高度。要知道单元格将位于何处。
- heightForRowAtIndexPath
- 最后它要求数据源给它一个单元格以显示在给定的索引路径
-cellForRowAtIndexPath
So you can't access to cells from heightForRowAtIndexPath
because with a most likely cell is not created yet.
因此您无法访问单元格,heightForRowAtIndexPath
因为最有可能的单元格尚未创建。
However in case I misunderstood your question go try:
但是,如果我误解了您的问题,请尝试:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
回答by skue
The obvious answer is to call cellForRowAtIndexPath
, but you may have already discovered there are some issues with that. See this question if you haven't already: UITableView flexible/dynamic heightForRowAtIndexPath
显而易见的答案是调用cellForRowAtIndexPath
,但您可能已经发现其中存在一些问题。如果你还没有看到这个问题:UITableView flexible/dynamic heightForRowAtIndexPath
For my last project I used a custom subclass of UICell and implemented a method like this. I then called this from table:heightForRowAtIndexPath:
(after looking up the content for that row).
在我的上一个项目中,我使用了 UICell 的自定义子类并实现了这样的方法。然后我从table:heightForRowAtIndexPath:
(在查找该行的内容之后)调用它。
+ (CGFloat) heightOfContent: (NSString *)content
{
CGFloat contentHeight =
[content sizeWithFont: DefaultContentLabelFont
constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
lineBreakMode: UILineBreakModeWordWrap].height;
return contentHeight + DefaultCellPaddingHeight;
}
回答by Jhaliya
i would suggest you to calculate the correct height of a row in table:heightForRowAtIndexPath:
by using your data structure (Height of text,images,mutliline text etc..).
我建议您table:heightForRowAtIndexPath:
使用您的数据结构(文本、图像、多行文本等的高度)来计算行的正确高度。
And change the component height in their -(void) layoutSubviews
method.
并在他们的-(void) layoutSubviews
方法中更改组件高度。
回答by Prajwal Udupa
If you want to access the cells use tags. But as @Jhaliya has said it is better to calculate the height based on the content of the cell. If your regard is with the position of the cell then I would suggest you to use scroll view and implement the cells yourself wherever you want.
如果要访问单元格,请使用标签。但正如@Jhaliya 所说,最好根据单元格的内容计算高度。如果您关注单元格的位置,那么我建议您使用滚动视图并在您想要的任何地方自己实现单元格。