我是 iphone 开发新手。我正在解析一个 xml 文件并在表格的每一行中显示标题、日期、视图和摘要。摘要的内容很大,所以单元格中只显示前 3 个单词。如何我根据内容的长度增加了行的高度。所有内容都应该正确地放在单元格内,并且应该显示完整的内容。请帮帮我。谢谢。
问问题
10137 次
5 回答
13
您可以在 tableView.delegate/datasource 中执行此操作:
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row){
case 0:
if(indexPath.section==0)
return 123.0; // first row is 123pt high
default:
return 40.0; // all other rows are 40pt high
}
}
然而,这确实会降低滚动性能。理想情况下,您应该使用 将所有行的行高设置为相同tableview.rowHeight
。
于 2010-02-12T15:18:17.703 回答
2
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = @"write your dynamic text here";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(320.0f, 999); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
于 2014-09-05T12:30:41.510 回答
1
如果您在一个视图控制器中有多个表视图,您可以按照以下方法为每个不同的表视图执行操作。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.tableViewCampaigns) {
return 45;
}else if(tableView == self.tableViewAgenda)
return 32;
else
return 29;
}
于 2017-02-21T05:36:51.367 回答
0
一种方法是手动设置单元格的大小。只需用合适的东西替换 ROW_HEIGHT 即可。
cell.frame = CGRectMake(0.0, 0.0, 320.0, ROW_HEIGHT);
于 2010-02-12T15:11:44.760 回答
0
尝试使用根据单元格内容自动管理的委托。
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
于 2017-02-21T07:13:42.910 回答