iphone - 设置 UITableViewCell 文本的宽度

标签 iphone ios uitableview

我有一个带有 uitableviewcells 的 uitableview,但是我的一些文本太长了,它一直延伸到单元格的边缘,我的附件勾选 View 将在选择时出现。

如果文本超出宽度并进入第二行,我目前正在换行。但是我希望有一个很好的方法来限制这个 UITableViewCell 标签的宽度。

最佳答案

在您的 cellForRowAtIndexPath: 函数中。第一次创建单元格时:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil)
 {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
   cell.textLabel.numberOfLines = 0;
   cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
 }   

现在指定 UITableViewCell 的大小,在 heightForRowAtIndexPath 函数中执行此操作:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *str = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label  
  CGSize maximumSize = CGSizeMake(300, 100); // change width and height to your requirement
 CGSize strSize = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap] //dynamic height of string depending on given width to fit

 return (10+strSize.height+10) // caculate on your bases as u have string height
}

关于iphone - 设置 UITableViewCell 文本的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085174/

相关文章:

ios - 如何更改 UISegmentedControl 的高度?

iphone - 使用上一篇文章的解决方案自定义分组 TableView 的背景/边框颜色

ios - XCode 中的 Clang 错误

ios - reloadRowsAtIndexPaths 搞乱了 animateWithDuration

ios - 在对 tableView 约束进行动画处理时,TableViewCells 最初的大小错误

ios:如何在 View Controller 中保留对象值

ios - 如何使用 Parse 和 Swift 向帖子添加评论

iphone - 一次仅选择一个按钮

ios - 解析查询后重新加载 tableView(串行队列?)

ios - renderInContext 对应用程序内存征税