views:

59

answers:

3

Is it possible to modify height of only one cell in a grouped table view?
I have a table view with 2 sections of 3 and 2 rows... I would change row height of the second row of the second section...

How can I do this?
Thanks!

+1  A: 

Have a look at

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

For the indexpath just check for which row and section and adjust the height accordingly

Liam
+4  A: 

You can look at this method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

In your case, the code should look like:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (indexPath.section == 1 && indexPath.row == 1) {
      return SPECIAL_HEIGHT;
   }
   return NORMAL_HEIGHT;
}

You can look for more details about the method here

vodkhang
Ok thanks! But I have a problem... If I type a number like return 60.0; works greatly but if I type some code for returning the height based on a UILabel text, cell background don't appear... I added this code in the cellForRowAtIndexPath method:
Matthew
else if (indexPath.section == 1 descrizioneLabel.font = [UIFont boldSystemFontOfSize:14.0]; descrizioneLabel.backgroundColor = [UIColor clearColor]; descrizioneLabel.numberOfLines = 0; descrizioneLabel.text = wish.descrizione; [cell.contentView addSubview:descrizioneLabel]; }
Matthew
and this is the method posted by you: -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1 UIFont *cellFont = [UIFont boldSystemFontOfSize:14.0]; CGSize constraintSize = CGSizeMake(280.0, MAXFLOAT); CGSize labelSize = [testo sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; return labelSize.height; } return 44.0;}
Matthew
I am not sure but here is my guess: the method heightForRow is called before the method cellForRow, so at the time you call descrizoneLabel in heightForRow, it is not constructed yet, can you test that. I don't have XCode here
vodkhang
Ok now I understand... And how can I do to create the label and then call the heightForRow methods? Thanks ;)
Matthew
yeah, it is possible
vodkhang
Is it possible to create the label in the viewDidLoad method? Are this method called before the heightForRow method? How can I call tableViewCell from the viewDidLoad method?
Matthew
viewDidLoad is always called once when you init the view and you can create the view. you can create the label in viewDidLoad and then call `[self.tableView reloadData];`
vodkhang
I've just created the label in the viewDidLoad method and in the cellForRowAtIndexPath I added it to the cell with [cell.contentView addSubview:label] I also added some NSLogs in the heightForRow method and the code works because in the console I see the value! Now the cell change height but I use initWithFrame:CGRectMake(10, 11, 285, 50) to create the label then if the text is short, the label isn't centred with the cell and it exit from it... how can I retrieve the width and the height of the cell for use it in the initWithFrame method? Thanks :D If you don't understand me I'll email you ;)
Matthew
it should be better to email me, it started to be a long comment, `[email protected]`
vodkhang
Oh I solved the problem buy adding a method that calculate an height based on a text string then in the initWithFrame method I added the returned value :) Thanks a lot!
Matthew
ok. You are welcome
vodkhang