Reputation:
I have a table view which has prototype cells. How do I set the height of all cells using a specific identifier? For example, I have two cells; one with an idenfier of "cell10"
and another with the identifier "cell50"
. How do I set it so all cells with the identifier "cell10"
have a height of 10 while all cells with the identifier "cell50"
have a height of 50? Any answers are appreciated. (By the way, I am using Swift 2.)
Upvotes: 0
Views: 295
Reputation: 21
heightForRowAtIndexPath called first and then cellForRowAtIndexPath get called. So it may be possible that in heightForRowAtIndexPath method we do not get cell or some inconsistency.
You must have some value or field on basis of which you can specify or differentiate cell identifier. You have array of models which may contain that value. So in heightForRowAtIndexPath(), you can get model from datasource array for that indexpath using objectAtIndex method, and check which type it is and depending on that return the height.
This will help in every case. It is working for me.
Upvotes: 0
Reputation: 4232
I'm not sure how to do it in swift but in objective C i would do the following things-
Implement the following function of tableview-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//your custom cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.reuseIdentifier isEqualToString:@"cell10"){
return 10;
}else{
return 50;
}
}
Upvotes: 0
Reputation: 37590
Within heightForRowAtIndexPath() you can call cellForRowAtIndexPath(). Once you have the cell then you can call reuseIdentifier to get its identifier. Then return either 10 or 50 based on the identifier
Upvotes: 2