Custom Cell Row Height setting in storyboard is not responding

后端 未结 18 2113
名媛妹妹
名媛妹妹 2020-11-30 16:54

I am trying to adjust the cell height for one of the cells on my table view. I am adjusting the size from the \"row height\" setting inside the \"size inspector\" of the cel

18条回答
  •  佛祖请我去吃肉
    2020-11-30 17:22

    For dynamic cells, rowHeight set on the UITableView always overrides the individual cells' rowHeight.

    This behavior is, IMO, a bug. Anytime you have to manage your UI in two places it is prone to error. For example, if you change your cell size in the storyboard, you have to remember to change them in the heightForRowAtIndexPath: as well. Until Apple fixes the bug, the current best workaround is to override heightForRowAtIndexPath:, but use the actual prototype cells from the storyboard to determine the height rather than using magic numbers. Here's an example:

    - (CGFloat)tableView:(UITableView *)tableView 
               heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        /* In this example, there is a different cell for
           the top, middle and bottom rows of the tableView.
           Each type of cell has a different height.
           self.model contains the data for the tableview 
        */
        static NSString *CellIdentifier;
        if (indexPath.row == 0) 
            CellIdentifier = @"CellTop";
        else if (indexPath.row + 1 == [self.model count] )
            CellIdentifier = @"CellBottom";
        else
            CellIdentifier = @"CellMiddle";
    
        UITableViewCell *cell = 
                  [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        return cell.bounds.size.height;
    }
    

    This will ensure any changes to your prototype cell heights will automatically be picked up at runtime and you only need to manage your UI in one place: the storyboard.

提交回复
热议问题