Reputation: 17169
I have made up a few different custom cells in my UITableView in Interface Builder, including each of them having a custom height, larger than the default 44 pixels high.
I then load them up like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
for (int cell = 0; cell <= [tableCellsArray count]; cell++)
{
if ([indexPath row] == cell)
{
CellIdentifier = [tableCellsArray objectAtIndex:cell];
}
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
return cell;
}
They each have a custom class and in the code above I loop through an array which basically holds the associated cell identifiers. However upon running the app, all the cells return the default 44 pixel height.
Without using the delegate method to return my own cell height, is there not something I have missed which might cause this?
Thanks.
Upvotes: 3
Views: 5812
Reputation: 5130
In iOS 8 and above we can use the Dynamic Table View Cell Height.
Using this feature UITableviewCell get its height from its content and We don't need to write heightForRowAtIndexPath
All I have to do in viewDidLoad()
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
Setup constraint in cell:
Result:
Here we can see : when the text increases, Cell size also get increase automatically without writing heightForRowAtIndexPath
Upvotes: 0
Reputation: 25740
You can change the height of all of your cells in the definition of the tableView, but to set them individualy you have to use the delegate method. It's confusing that you can set it in IB but it is for display purposes only and not used at runtime.
Upvotes: 6
Reputation: 3376
You will have to implement the following selector and apply the proper logic so that based on your type of custom cell the correct height will be set. Without it the default height of 44 will be used
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return YOUR_WANTED_CELL_HEIGHT;
}
Don't think you missed anything else.
Upvotes: 5