Reputation: 17160
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.
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;
}
Upvotes: 15
Views: 14242
Reputation: 24041
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.
Upvotes: 0
Reputation: 38239
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.
Upvotes: 17
Reputation: 17005
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.
Upvotes: 1
Reputation: 9054
I would set the tag for each UITableView Cell so that in heightForRowAtIndexPath you'll be able to differentiate by the following:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.tableview cellForRowAtIndexPath:indexPath].tag == yourTag){
//perform your action for cell 1, 2, or 3
}
else if (etc...)
}
Upvotes: 0
Reputation: 17732
You would differentiate them exactly how you would in cellForRowAtIndexPath:
. If you aren't grouping them by section, then you'd have to poll your data for the particular indexPath in question, and determine the height after that.
Upvotes: 0