Increase the main tableview row height according to the custom cell

前端 未结 7 680
名媛妹妹
名媛妹妹 2021-01-03 07:32

I am having an app in which I have a Tableview and on that tableview\'s each row I am dynamically creating a custom tableview cell.

Below is the cod

7条回答
  •  离开以前
    2021-01-03 07:43

    Try this

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     return [self.tableName.dataSource tableView:self.tableName cellForRowAtIndexPath:indexPath].contentView.frame.size.height;
    }
    

    And in your cellForRowAtIndexPath delegate if you are using label then first set number of lines to label as 0, then set label text and then add sizeTofit property of it. Like this

     [cell.yourLabelName setNumberOfLines:0];
     cell.yourLabelName.text = @"Your long or short text";
     [cell.yourLabelName sizeToFit];
    

    This will expand your label height as per the text in it.

    After that you can set the size of your cell contentView like this.

     cell.contentView.frame = CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width,"Height of label" + cell.contentView.frame.size.height);
    

提交回复
热议问题