如何在没有自定义单元格的情况下在UITableViewCell中包装文本

Air*_*Ltd 151 cocoa-touch objective-c uikit iphone-sdk-2 ios

这是在iPhone 0S 2.0上.2.1的答案也很好,但我不知道有关表的任何差异.

感觉应该可以在不创建自定义单元格的情况下获取文本,因为默认情况下UITableViewCell包含a UILabel.我知道如果我创建一个自定义单元格,我可以使它工作,但这不是我想要实现的 - 我想了解为什么我当前的方法不起作用.

我已经发现标签是按需创建的(因为单元格支持文本和图像访问,所以它不会在必要时创建数据视图),所以如果我做这样的事情:

cell.text = @""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

然后我得到一个有效的标签,但设置numberOfLines(和lineBreakMode)不起作用 - 我仍然得到单行文本.UILabel文本中有足够的高度显示 - 我只是为高度返回一个较大的值heightForRowAtIndexPath.

Tim*_*upe 274

这是一种更简单的方法,它适用于我:

在你的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];
}
Run Code Online (Sandbox Code Playgroud)

您会注意到我将标签的行数设置为0.这使得它可以根据需要使用尽可能多的行.

下一部分是指定你的大小UITableViewCell,所以在你的heightForRowAtIndexPath函数中这样做:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20;
}
Run Code Online (Sandbox Code Playgroud)

我在返回的单元格高度上添加了20,因为我喜欢在文本周围添加一点缓冲区.

  • 您不需要将cell.textLabel.numberOfLines设置为某个任意高的数字.将其设置为0表示"显示所需的行数". (17认同)
  • UILineBreakModeWordWrap在iOS 6中已弃用.请改用NSLineBreakByWordWrapping. (17认同)

ddi*_*ego 16

更新了Tim Rupe对iOS7的回答:

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    NSAttributedString *attributedText =
        [[NSAttributedString alloc]
            initWithString:cellText
            attributes:@
            {
                NSFontAttributeName: cellFont
            }];
    CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    return rect.size.height + 20;
}
Run Code Online (Sandbox Code Playgroud)