ios - 当文本超出空间时使 UITableViewCell 展开

标签 ios iphone uitableview text expand

我有一个 UITableViewCell,它是 UITableViewCellStyleDefault。当我尝试放置比 UITableViewCell 更长的文本时,它会截断它。如何使单元格扩展以容纳此文本?

最佳答案

我还没有尝试完全按照您的要求去做,但可能会是这样的:

您需要根据 View 中文本字符串的长度来更改 View 的大小。

TableView 委托(delegate)(您的 View Controller )应该实现

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Message *msg = (Message *)[messages objectAtIndex:indexPath.row];
    CGSize size = [[msg text] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:kLabelFontSize]
                         constrainedToSize:CGSizeMake(220.0f, 480.0f)
                             lineBreakMode:UILineBreakModeTailTruncation];
    CGFloat minHeight = 20.0f;
    CGFloat height = size.height > minHeight ? size.height : minHeight;
    return height;
}

告诉 View 每行的高度。

您还需要类似地调整 UITableViewCell 标签框架的大小。

关于ios - 当文本超出空间时使 UITableViewCell 展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2528194/

相关文章:

ios - 应用程序 :didFinishLaunchesWithOptions: 中的长时间运行任务

iphone - 是否可以使用纯 C 在 iPhone 上捕获触摸事件?

ios - Swift - 如何为每个 View 设置不同的导航栏?

ios - 如何仅在单元格中禁用prepareForSegue 和DidSelectRow?

iphone - 触摸事件通过透明部分传递给superview

ios - 如何从 IOS 中的数组返回正确数量的对象

ios - 在表格 View 中滚动自定义表格单元格时出错

iphone - 动态添加和删除 UITableViewCells 到 UITableView

ios - 将 Xamarin.Forms 应用部署到 iOS : MT1006 EOF Error (error: 0xe800000e)

IOS 开发人员证书在到期日期之前已过期