How to resize UITableViewCell to fit its content?

前端 未结 8 764
有刺的猬
有刺的猬 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条回答
  •  猫巷女王i
    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;
        }
    

提交回复
热议问题