Custom height UITableViewCell

I created several different custom cells in my UITableView in Interface Builder, including each one with a custom height greater than the 44px maximum height.

Then I load them like this:

- (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 their own class, and in the above code, I am looping through an array that basically contains associated cell ids. However, when the application starts, all cells return the default height of 44 pixels.

Without using the delegate method to return my own cell height, am I missing anything that might cause this?

Thank.

+3


source to share


3 answers


You can change the height of all your cells in the tableView definition, but to set them individually, you must use the delegate method. It's confusing that you can set it to IB, but it is for display only and is not used at runtime.



+6


source


You will need to implement the following selector and apply the correct logic so that the correct height is set based on your custom cell type. Without it, the default height of 44 will be used.

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

    return YOUR_WANTED_CELL_HEIGHT;
}

      



Don't feel like you've missed anything else.

+5


source


In iOS 8 and above, we can use the Dynamic Table Cell Size Table .

With this function UITableviewCell get the height from its content and we don't need to write heightForRowAtIndexPath

All I need to do in viewDidLoad ()

tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;

      

Installation limit in cell:

enter image description here

Result:

enter image description here

Here we see: when the text grows, the size of the Cell also grows automatically without writing heightForRowAtIndexPath

0


source







All Articles