How to resize UITableViewCell to fit its content?

前端 未结 8 731
有刺的猬
有刺的猬 2020-12-08 05:34

I have a UITableview with multiple reusable TableViewCells. In one cell I have a UITextView, that resizes itself to fit its content. Now I \"just\

相关标签:
8条回答
  • 2020-12-08 05:59

    Here's an updated version for iOS 7+ that is cleaner (no extra method)

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            UIFont * font = [UIFont systemFontOfSize:15.0f];
            NSString *text = [getYourTextArray objectAtIndex:indexPath.row];
            CGFloat height = [text boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width, maxHeight) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName: font} context:nil].size.height;
    
            return height + additionalHeightBuffer;
        }
    
    0 讨论(0)
  • 2020-12-08 06:01

    In case of UILabel subview in the UITableViewCell, I accomplished auto resize of the label just by setting the label's constraints (using storyboard, Xcode 8.3.2).

    This is working since apparently the label's and the cell's default behavior is sizeToFit. Same should work for UITextView as well.

    0 讨论(0)
提交回复
热议问题