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

Ios 如何在没有自定义单元格的UITableViewCell中包装文本,ios,objective-c,cocoa-touch,uikit,iphone-sdk-2,Ios,Objective C,Cocoa Touch,Uikit,Iphone Sdk 2,这是在iPhone0s2.0上。2.1的答案也不错,尽管我不知道在表格方面有什么不同 由于默认情况下,UITableViewCell包含一个UILabel,因此感觉应该可以在不创建自定义单元格的情况下将文本换行。我知道如果我创建一个自定义单元格,我可以让它工作,但这不是我想要实现的-我想了解为什么我目前的方法不起作用 我发现标签是按需创建的(因为单元格支持文本和图像访问,所以在必要时才创建数据视图),所以如果我这样做: cell.text = @""; // create the label U

这是在iPhone0s2.0上。2.1的答案也不错,尽管我不知道在表格方面有什么不同

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

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

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

然后我得到了一个有效的标签,但是在上面设置
numberOfLines
(和lineBreakMode)不起作用-我仍然得到单行文本。
UILabel
中有足够的高度供文本显示-我只是在
heightforrowtaindexpath

中返回一个较大的高度值,我认为您无法操纵基本
UITableViewCell的
private
UILabel
来执行此操作。您可以自己向单元格中添加一个新的
UILabel
,并使用
numberOfLines
sizeToFit
来适当调整其大小。比如:

UILabel* label = [[UILabel alloc] initWithFrame:cell.frame];
label.numberOfLines = <...an appriate number of lines...>
label.text = <...your text...>
[label sizeToFit];
[cell addSubview:label];
[label release];
UILabel*label=[[UILabel alloc]initWithFrame:cell.frame];
label.numberOfLines=
label.text=
[标签尺寸合适];
[单元格添加子视图:标签];
[标签发布];

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

在您的
单元格中,按行索引路径:
函数。第一次创建单元格时:

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。这使得它可以根据需要使用任意多的行

下一部分是指定
UITableViewCell
的大小,因此在
heightforrowatinexpath
函数中:

- (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,因为我喜欢文本周围有一点缓冲。

我认为这是一个更好、更短的解决方案。只需格式化单元格的
UILabel
textLabel
)即可通过指定
sizeToFit
自动计算高度,一切都应该正常

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = @"Whatever text you want to put here is ok";
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    [cell.textLabel sizeToFit];

    return cell;
}

一个简短的评论/回答,记录我遇到同样问题时的经历。尽管使用了代码示例,表视图单元格高度仍在调整,但单元格内的标签仍未正确调整-解决方案是我从自定义NIB文件加载单元格,这发生在调整后的单元格高度之后

我在NIB文件中设置了不换行文本,标签只有一行;NIB文件设置覆盖了我在代码中调整的设置


我上的课是要确保始终牢记对象在每个时间点的状态-它们可能还没有被创建。。。hth下面有人。

如果我们只在
UITableView
单元格中添加文本,我们只需要两个代理即可(无需添加额外的
UILabels

1)
cellforrowatinexpath

2)
rowAtIndexPath高度

这个解决方案对我有效:-

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{ 
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;

    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
    cell.textLabel.text = [mutArr objectAtIndex:indexPath.section];
    NSLog(@"%@",cell.textLabel.text);

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png" ]];

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    CGSize labelSize = CGSizeMake(200.0, 20.0);

    NSString *strTemp = [mutArr objectAtIndex:indexPath.section];

    if ([strTemp length] > 0)
        labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];

    return (labelSize.height + 10);
}
这里的字符串
mutArr
是一个可变数组,我从中获取数据

编辑:-这是我拍摄的数组

mutArr= [[NSMutableArray alloc] init];

[mutArr addObject:@"HEMAN"];
[mutArr addObject:@"SUPERMAN"];
[mutArr addObject:@"Is SUPERMAN powerful than HEMAN"];
[mutArr addObject:@"Well, if HEMAN is weaker than SUPERMAN, both are friends and we will never get to know who is more powerful than whom because they will never have a fight among them"];
[mutArr addObject:@"Where are BATMAN and SPIDERMAN"];

我使用以下解决方案

数据在成员中单独提供:

-(NSString *)getHeaderData:(int)theSection {
    ...
    return rowText;
}
处理可以在
cellforrowatinexpath
中轻松完成。 定义单元格/定义字体并将这些值指定给结果“单元格”。 请注意,
numberoflines
设置为“0”,这意味着需要什么就拿什么

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
    cell.textLabel.text= [self getRowData:indexPath.section];
    cell.textLabel.font = cellFont;
    cell.textLabel.numberOfLines=0;
    return cell;
}
heightforrowatinexpath
中,我计算包装文本的高度。 身体的大小应与你的细胞的宽度有关。对于iPad,该值应为1024。 对于iphoneenipod320

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
    CGSize boundingSize = CGSizeMake(1024, CGFLOAT_MAX);
    CGSize requiredSize = [[self getRowData:indexPath.section] sizeWithFont:cellFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    return requiredSize.height;    
}

更新了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;
}

我发现这非常简单明了:

[self.tableView setRowHeight:whatEvereight.0f];
例如:

[self.tableView setRowHeight:80.0f];

这可能是最好的/标准的方法,也可能不是,但在我的情况下,它起到了作用。现在,TableView可以自动调整单元格大小。设置表格视图,如下所示

tableView.estimatedRowHeight=85.0//使用适当的估计值
tableView.rowHeight=UITableViewAutomaticDimension


用swift试试我的代码。此代码也适用于普通UILabel

extension UILabel {
    func lblFunction() {
        //You can pass here all UILabel properties like Font, colour etc....
        numberOfLines = 0
        lineBreakMode = .byWordWrapping//If you want word wraping
        lineBreakMode = .byCharWrapping//If you want character wraping
    }
}
现在就这样简单地打电话

cell.textLabel.lblFunction()//Replace your label name 

您不需要将cell.textLabel.numberOfLines设置为任意高的数字。将其设置为0意味着“显示所需的行数”。谢谢,在我有机会在我自己的项目中验证后,我今晚将编辑我的帖子。将行数设置为0效果很好(显示所需的行数)。卡格林:事实证明,我可以复制这一点,但前提是我在模拟器中使用iPhone OS 3.0。当我使用3.1+时,detailTextLabel的大小与sizeWithFont的大小非常匹配。因此,Tim的方法非常有效-仅适用于3.1+(可能是由于3.0默认单元渲染中的错误/错误特性)。作为记录,我使用的垂直上/下边距为12(每个),详细文本标签大小为(188.0,CGFLOAT_MAX),粗体SystemFontOfSize为15。iOS 6中不推荐使用UILineBreakModeWordWrap。改为使用NSLineBreakByWordWrapping。据我所知,rowHeight属性设置了一个固定高度,该高度将用于表视图中的所有单元格。在大型表中使用rowHeight有助于提高性能,但如果您需要每个单元格的高度根据其内容而变化,那么似乎必须使用tableView:heightforrowatinexpath:method。