0

I am trying to create a sample app to resize a cell. The cell is staying at the default height of 44px. I am unable to make this change through the storyboard because I have a view with varying height inside the cell. Can this be done with only 1 cell? I have an outlet from the table cell to the variable called cell. My code in the .m file is as follows. Also, all the cells are already static.

    self.whereCell.frame = CGRectMake(0, 191, 320, 78);
Community
  • 1
  • 1

1 Answers1

1

Have you tried the tableView:heightForRowAtIndexPath: method?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 191;
    }

    return 44;
}

This shouldn't been needed if the cell is static though; you should be able to set the height of the cell in Interface Builder.

Joseph Duffy
  • 4,566
  • 9
  • 38
  • 68
  • Hmm.. that did help, but what if we wanted to resize the cell relative to this view inside the cell? In this sample app, the view inside the app could have 2 or 3 lines of text. As a result, the cell could be either too big or too small. Is there a way to make the cell exactly, say 27px bigger than the view? –  Mar 21 '15 at 22:33
  • Also, like I explained in the post, I cannot set the height of the cell in IB because it is also related to a view inside. –  Mar 21 '15 at 22:35
  • The easiest way (although there is no _easy_ way) is to use auto layout, but you may still have to calculate the height yourself and return it. In iOS 8 you shouldn't have to worry about that as much but I've found that it's still not perfect – Joseph Duffy Mar 21 '15 at 22:35
  • I have a subclass of `UITableView` I use within my apps that I have open sourced. It requires a little bit of a change in the way you populate your table but does a lot of the heavy lifting. It's written in Swift but feel free to take a look: [Dynamic Controls](https://github.com/YetiiNet/DynamicControls) – Joseph Duffy Mar 21 '15 at 22:37
  • Is there a way to access the height of the label, say like: –  Mar 21 '15 at 22:41
  • self.label.frame.size.height + 27 –  Mar 21 '15 at 22:41
  • Technically, yes, you could get the frame of the label, but the label may not know its exact height/width at the time of that method being called. Take a look at the link I posted in my earlier comment or check out [an answer to a question on using auto layout within cells](http://stackoverflow.com/a/18746930/657676) – Joseph Duffy Mar 21 '15 at 22:47