Different UITableViewCell heights

14,178

Solution 1

of course it crashed, because you are in a delegate method, (you are a delegate of the UITableView) and from such method calling back the UITableView methods takes very high risk of crash.

the method ([tableView cellForRowAtIndexPath:indexPath]), what you call, will call a delegate method again for the cell's height, and it causes an infinite loop, aka crash.

this is normal behaviour.

the key is in your original data source, you could store the height for each row in the original data source, and you can read everything back from there, in your delegate methods without any risk.

your code fragment does not say where your data source is and what kind of your data source is, thus I cannot give more exact solution in lack of it, but the idea would be that.

Solution 2

Updated

Use dynamic height of UITableView now as it's easy as UITableView automatically calculates it

Old

Use this method to your requirement below is an example:

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

  //calculate height according to text and on basis of indexPath
  if(indexpath.row == 0)
  {
    return 60.0f;
  }
  else if(indexpath.row == 1)
  {
    return 70.0f;
  }
  else
  {
    return 55.0f;
  }
}

Note : if your size is not fixed then you will have calculate it then provide in above method according to your requirement.

Solution 3

You shouldn't call cellForRowAtIndexPath within heightForRowAtIndexPath or you'll enter an infinite loop which will cause the app crash.

You should determine the height of every cell in the same way you determine the type of the same cell in cellForRowAtIndexPath.

That's the only solution I think.

Share:
14,178
Josh Kahane
Author by

Josh Kahane

Updated on June 12, 2022

Comments

  • Josh Kahane
    Josh Kahane almost 2 years

    I have built three different cells in my storyboard and hooked all the outlets up, each cell has a unique identifier.

    For example I have one cell which holds a picture, another which has a label and another with other contents, so they are all unique and each cell type requires its own height (dynamic or status, it doesn't matter).

    However, how is it that I can make a cell with 'indentifier1' return a certain height and then the others cells returns different heights?

    I know I can use - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath but I am unsure how to differentiate the cells.

    I am using core data and fetch results for the tableview from that.

    Edit

    I have tried this with tags but its crashes at the first if statement:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGFloat cellHeight;
    
        if ([[tableView cellForRowAtIndexPath:indexPath] tag] == 1) cellHeight = 170;
        else if ([[tableView cellForRowAtIndexPath:indexPath] tag] == 2) cellHeight = 100;
        else if ([[tableView cellForRowAtIndexPath:indexPath] tag] == 3) cellHeight = 140;
    
        return cellHeight;
    }
    
  • Josh Kahane
    Josh Kahane almost 12 years
    I forgot to mention, I don't know what cells are at which index, its user content listed here. So this wouldn't work.
  • Josh Kahane
    Josh Kahane almost 12 years
    Edited question, I have tried tags but failed (see my attempt in my question).
  • Apollo
    Apollo almost 12 years
    right but how are you setting these tags? could you post that?
  • Apollo
    Apollo almost 12 years
    hm I usually set my tags in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, however you should be able to set tags in storyboard...
  • Josh Kahane
    Josh Kahane almost 12 years
    Thats what I thought. It doesn't like this at all: [self.tableView cellForRowAtIndexPath:indexPath]. Now this is a mere hunch but would dequeueReusableCellWithIdentifier dislike this?
  • Apollo
    Apollo almost 12 years
    no that shouldn't affect anything. Are you setting the tableview delegate and datasource properly? Other than that this should work fine...
  • Josh Kahane
    Josh Kahane almost 12 years
    Yep the datasource and delegate are properly connect, if I run the app without those if statements and just return a raw value for cell height it runs fine. Which why I am confused.
  • Josh Kahane
    Josh Kahane almost 12 years
    Apparently: The problem is that heightForRowAtIndexPath is called before cellForRowAtIndexPath. So the cell has not been created yet. So how do I get around this?
  • Apollo
    Apollo almost 12 years
    is there anyway to index your data before hand? can you force the user to have the image label in the first cell, text label the next and so on?