Current cell height inside heightForRowAtIndexPath?

I have a table with static cells. For one cell, I want to change its height depending on the height of the label (inside this cell) and at the same time leave all other cell heights intact. How can I get the current cell height? Or maybe there is a better approach?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath section] == 2) { return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; } else { return ...; // what should go here, so the cell doesn't change its height? } } 
+8
ios objective-c uitableview
source share
6 answers

You may call:

 [super tableView:tableView heightForRowAtIndexPath:indexPath] 

in the else block, so you don’t have to worry if you changed the default height.

+12
source share

You can get / set the default height of tableView.rowHeight , or you can save the height before changing the height of the cell so that you can get the default height from some variable;

+4
source share

Please do it

 if ([indexPath section] == 2) { if(indexPath.row == 1) return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; else tableView.rowHeight } 
+1
source share

@talnicolas is a great answer, here for Swift 3:

 override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.section == 2 { let easy = self.myLabel.frame return easy.origin.y *2 + easy.size.height } else { return super.tableView(tableView, heightForRowAt: indexPath) } } 
+1
source share

You might want to calculate the height of the mark in a limited width. In this case, you can create a method like this:

 - (CGFloat)textHeightOfMyLabelText { CGFloat textHeight = [self.myLabel.text sizeWithFont:self.myLabel.font constrainedToSize:LABEL_MAX_SIZE lineBreakMode:self.myLabel.lineBreakMode].height; return textHeight; } 

and use the result in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath . Remember to add the marker value of your label.

0
source share
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == youSectionNumber) { if (indexPath.row == numberOfrow) { return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; } } return 44; // it is default height of cell; } 
0
source share

All Articles