我需要做一个UITableViewCell
其中包含大量文本。我知道我可以添加一个UITextView
到我的单元格,但每个条目的文本量不同。
我知道我可以控制高度UITableViewCell
with: -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
,但这并不是我真正想要的。
场景一:
---------------
| First Cell |
---------------
---------------
| Second Cell |
| with some |
| text. |
---------------
---------------
| Third Cell |
---------------
场景2:
---------------
| First Cell |
---------------
---------------
| Second Cell |
| with some |
| more text and |
| an unknown |
| cell height. |
---------------
---------------
| Third Cell |
---------------
Use UILabel
为您的单元格文本。然后您可以使用sizeWithFont:constrainedToSize:
计算每个单元格内 UILabel 的高度。例如:
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)