我的表视图的单元格可以保存最多140个字符,因此对于我的UITableView中的某些单元格,高度需要略微增加。我不是在寻找什么花哨,140个字符将需要的细胞增加的60返回“cellForRowAtIndexPath”函数的单元格大小?
我看到这个堆栈溢出后约两倍默认高度: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
,并下载了iOS 7示例项目只能找到动态设置单元高度的50多个独特功能。对于140个字符的罕见消息,这真的是必要的吗?
难道我不能简单地设置在这个非常功能的细胞高度?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chatCell" forIndexPath:indexPath];
// Configure the cell...
NSDictionary *message = self.messages[indexPath.row];
UILabel *lblUsername=(UILabel *)[cell viewWithTag:1];
UILabel *lblBody=(UILabel *)[cell viewWithTag:2];
lblUsername.text = [message valueForKeyPath:@"author"];
lblBody.text = [message valueForKeyPath:@"body"];
return cell;
}
我只需要执行一个if语句是这样的:
if (lblBody.text.length <= 25) {
// there's little text, keep the default height
} else if (lblBody.text.length <= 50) {
// make the height of this cell slightly bigger
} else if (lblBody.text.length <= 75) {
// make the height of this cell moderately bigger
} else {
// make the height of this cell large
}
//etc...
return cell;
从而为这部分工作完成。这可能吗?
不是所有的字符都是相同的长度。这个问题比你所期望的更复杂。 – CrimsonChris
'heightForRowAtIndexPath'被调用BEFORE'cellForRowAtIndexPath'。改变'cellForRowAtIndexPath'中的高度将不起作用。 – CrimsonChris
使用原型单元更简单。这是一个例子。 http://www.macspotsblog.com/dynamic-uitableview-cell-heights-programmatically/ – CrimsonChris