繁体   English   中英

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

[英]How do I wrap text in a UITableViewCell without a custom cell

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

感觉好像应该可以在不创建自定义单元的情况下自动换行,因为UITableViewCell默认包含UILabel 我知道如果创建自定义单元,我可以使其工作,但这不是我要实现的目标-我想了解为什么我当前的方法不起作用。

我发现标签是按需创建的(由于单元格支持文本和图像访问,因此它直到需要时才创建数据视图),因此如果执行以下操作:

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

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

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

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

您会注意到,我将标签的行数设置为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;
}

我在返回的单元格高度上增加了20,因为我喜欢在文本周围留一些缓冲。

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

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

而且我在NIB文件中设置了不换行的设置,标签只有1行。 NIB文件设置覆盖了我在代码中调整的设置。

我上的课是确保始终记住对象在每个时间点的状态-它们可能尚未创建! ...让某人下线。

如果我们只在UITableView单元格中添加文本,则只需要两个委托即可使用(不需要添加额外的UILabels

1) cellForRowAtIndexPath

2) heightForRowAtIndexPath

这个解决方案为我工作:

-(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"];

现在,表视图可以具有自动调整大小的单元格。 如下设置表格视图

tableView.estimatedRowHeight = 85.0 //use an appropriate estimate tableView.rowHeight = UITableViewAutomaticDimension

苹果参考

我使用以下解决方案。

数据在成员中单独提供:

-(NSString *)getHeaderData:(int)theSection {
    ...
    return rowText;
}

该处理可以在cellForRowAtIndexPath轻松完成。 定义单元格/定义字体,并将这些值分配给结果“ cell”。 需要注意的是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;
}

heightForRowAtIndexPath ,我计算heightForRowAtIndexPath文本的高度。 绑定大小应与单元的宽度有关。 对于iPad,该值为1024。对于iPhone和iPod 320。

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

我发现这是非常简单直接的:

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

例如:

[self.tableView setRowHeight:80.0f];

这样做可能不是最好/标准的方法,但在我看来,它是可行的。

快速尝试我的代码。 此代码也适用于普通的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 

我认为这是一个更好,更短的解决方案。 只需设置单元格的UILabeltextLabel )的格式,以通过指定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;
}

我认为您无法操纵基本UITableViewCell's私有UILabel来执行此操作。 您可以自己向该单元格添加一个新的UILabel ,并使用带有sizeToFit 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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM