如何在没有自定义单元格的情况下将文本包装在 UITableViewCell 中 [英] How do I wrap text in a UITableViewCell without a custom cell

查看:9
本文介绍了如何在没有自定义单元格的情况下将文本包装在 UITableViewCell 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is on iPhone 0S 2.0. Answers for 2.1 are fine too, though I am unaware of any differences regarding tables.

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

It feels like it should be possible to get text to wrap without creating a custom cell, since a UITableViewCell contains a UILabel by default. I know I can make it work if I create a custom cell, but that's not what I'm trying to achieve - I want to understand why my current approach doesn't work.

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

I've figured out that the label is created on demand (since the cell supports text and image access, so it doesn't create the data view until necessary), so if I do something like this:

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

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

then I get a valid label, but setting numberOfLines on that (and lineBreakMode) doesn't work - I still get single line text. There is plenty of height in the UILabel for the text to display - I'm just returning a large value for the height in heightForRowAtIndexPath.

推荐答案

这是一个更简单的方法,它对我有用:

Here is a simpler way, and it works for me:

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

Inside your cellForRowAtIndexPath: function. The first time you create your cell:

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];
}

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

You'll notice that I set the number of lines for the label to 0. This lets it use as many lines as it needs.

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

The next part is to specify how large your UITableViewCell will be, so do that in your heightForRowAtIndexPath function:

- (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;
}

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

I added 20 to my returned cell height because I like a little buffer around my text.

这篇关于如何在没有自定义单元格的情况下将文本包装在 UITableViewCell 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆