【问题标题】:Label in Custom TableViewCell can't be multiline or wrap自定义 TableViewCell 中的标签不能是多行或换行
【发布时间】:2013-03-26 10:36:55
【问题描述】:

我尝试将自定义 tableViewCell 类中的单元格设置为多行并换行。 我在这里阅读了我的问答并按照说明进行操作,但仍然找不到解决方案。

我可以调整每个内容长度的动态高度,但仍然无法使 这是我的代码

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell"];
   if(cell == nil){
      cell = [[CustomItemCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"CustomItemCell"];

      //I set lineBreakMode of cell.postLabel to NSLineBreakByWordWrapping 
      // and set numberofLines = 0 ; 
      cell.postLabel.lineBreakMode = NSLineBreakByWordWrapping;
      cell.postLabel.numberOfLines = 0;
    }
    CustomItem *item = [[channel items] objectAtIndex:[indexPath row]];
   //postLabel is the label that I want to be multiline and wordwrap  
   // postLabel get an NSString from [item post] 
   [[cell postLabel] setText:[item post]];

   [[cell timeLabel] setText:[item time]];
   [[cell distanceLabel] setText:[item distance]];
   [[cell thumbnailView] setImage:[item thumbnail]];

   return cell;
}

我创建了CustomItemCell.xib 文件。实际上,我还在界面生成器中将行设置为零并将换行符设置为自动换行,但它没有像我预期的那样显示。

【问题讨论】:

  • 如果您根据其内容设置标签高度,则标签可以是多行并换行
  • 如果postLabel 有问题,最简单的尝试方法是在设置文本后致电[[cell postLabel] sizeToFit]。还要小心将postLabel 添加到单元格的contentView
  • 高度已经设置,但没有任何改变。
  • Marko,我在 CustomItemCell.xib 中设置了我的单元格。我是否必须再次将其添加到内容视图中?

标签: ios iphone uitableview cocoa-touch uilabel


【解决方案1】:

试试这个,

[cell.postLabel sizeToFit];

【讨论】:

  • 已经试过了。但是没有用......我把它放在下面 [[cell postLabel] setText:[item post]];行。
  • 我发现了问题。界面构建器启用了“使用自动布局”。我禁用了这个选项,sizeToFit 完美运行
【解决方案2】:

为多行设置单元格样式如下图,

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

检查一下..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多