How does one obtain the UITableViewCell
when within the heightForRowAtIndexPath
method, i.e. given the 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?
You can use the tableview's delegate instead of the tableView itself.
id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
Check the answer here
In new iOS versions and using correct auto constraints you don't need to reference the cell anymore to calculate the cell's height.
Check here
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
This question has no sense because in
heightForRowAtIndexPath
no cells are created yet. Here's how tableView works:
-numberOfSectionsInTableView
-numberOfRowsInSection
- heightForRowAtIndexPath
-cellForRowAtIndexPath
So you can't access to cells from heightForRowAtIndexPath
because with a most likely cell is not created yet.
However in case I misunderstood your question go try:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
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
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).
+ (CGFloat) heightOfContent: (NSString *)content
{
CGFloat contentHeight =
[content sizeWithFont: DefaultContentLabelFont
constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
lineBreakMode: UILineBreakModeWordWrap].height;
return contentHeight + DefaultCellPaddingHeight;
}
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..).
And change the component height in their -(void) layoutSubviews
method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With