Custom UITableView Dynamic Cell Height

前端 未结 6 716
一个人的身影
一个人的身影 2021-02-06 09:22

I have search and searched through endless blogs and articles on how to determine a dynamic height for a custom UITableViewCell and its detailed text. I have really had a hard t

6条回答
  •  感动是毒
    2021-02-06 10:00

    I had a similar issue a while back and this helped me tremendously.

    #define PADDING 10.0f
    - (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *text = [self.items objectAtIndex:indexPath.row];
        CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
    
        return textSize.height + PADDING * 3;
    }
    

提交回复
热议问题