Different UITableViewCell heights
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.
Josh Kahane
Updated on June 12, 2022Comments
-
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 almost 12 yearsI 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 almost 12 yearsEdited question, I have tried tags but failed (see my attempt in my question).
-
Apollo almost 12 yearsright but how are you setting these tags? could you post that?
-
Apollo almost 12 yearshm 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 almost 12 yearsThats what I thought. It doesn't like this at all:
[self.tableView cellForRowAtIndexPath:indexPath]
. Now this is a mere hunch but woulddequeueReusableCellWithIdentifier
dislike this? -
Apollo almost 12 yearsno that shouldn't affect anything. Are you setting the tableview delegate and datasource properly? Other than that this should work fine...
-
Josh Kahane almost 12 yearsYep 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 almost 12 yearsApparently:
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 almost 12 yearsis 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?