How to dynamically resize UITableViewCell height

后端 未结 4 1077
野的像风
野的像风 2020-11-27 10:54

I have the classical grouped UITableView with editable UITextViews inside every cell. This text views can be single or multi-lined, and I want the cell to increase its heigh

相关标签:
4条回答
  • 2020-11-27 11:36

    To be more specific, yes, you have to implement tableView:heightForRowAtIndexPath: to calculate the new height, then do as rickharrison says and call [tableView reloadRowsAtIndexPaths:withRowAnimation]. Lets say your cells can have an expanded height and a normal height, and you want them to grow when tapped on. You can do:

    -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath 
    {
        if ([expandedPaths containsObject:indexPath]) {
            return 80;
        } else {
            return 44;
        }
     }
    
    -(void)tableView:(UITableView*) didSelectRowAtIndexPath:(NSIndexPath*) indexPath
    {
        [expandedPaths addObject:indexPath];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    0 讨论(0)
  • 2020-11-27 11:37

    You do not always have to reload the entire table. You can instead just reload that one row.

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    
    0 讨论(0)
  • 2020-11-27 11:38

    -reloadRowsAtIndexPaths:withRowAnimation did not resize the UITableViewCell height, even after I changed the Cell's frame. It only worked when I followed it with a -reloadData:

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    [tableView reloadData];
    
    0 讨论(0)
  • 2020-11-27 11:43
    [tableView beginUpdates];
    [tableView endUpdates];
    
    0 讨论(0)
提交回复
热议问题