Wri*_*sCS 28 iphone uitableview uitextview ios
我需要制作一个UITableViewCell包含大量文本的内容.我知道我可以添加UITextView到我的单元格,但每个条目都不会有相同数量的文本.
我知道我可以控制UITableViewCellwith 的高度:-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,但那不是我想要的.
场景1:
---------------
| First Cell |
---------------
---------------
| Second Cell |
| with some |
| text. |
---------------
---------------
| Third Cell |
---------------
Run Code Online (Sandbox Code Playgroud)
场景2:
---------------
| First Cell |
---------------
---------------
| Second Cell |
| with some |
| more text and |
| an unknown |
| cell height. |
---------------
---------------
| Third Cell |
---------------
Run Code Online (Sandbox Code Playgroud)
Anh*_* Do 34
使用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;
}
Run Code Online (Sandbox Code Playgroud)
解决的办法很简单,因为iOS的应该工作肯定7.确保该Scrolling Enabled选项被关闭了UITextView里面的UITableViewCell故事板。
然后在您的 UITableViewController 的 viewDidLoad() 中设置tableView.rowHeight = UITableViewAutomaticDimension和tableView.estimatedRowHeight > 0例如:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
}
Run Code Online (Sandbox Code Playgroud)
就是这样。UITableViewCell的高度将根据内部UITextView的高度自动调整。
| 归档时间: |
|
| 查看次数: |
15532 次 |
| 最近记录: |