ios - 单元格高度和换行符

标签 ios uitableview tableview

我正在将 iOS5 项目更新为 storybaord 并与 iOS6/iOS7 兼容运行。我的表格 View 有 4 个部分,每个部分都有不同长度的数据,这些数据可能会也可能不会显示为一行。

旧代码是这样的,我收到一条警告

CGSize size = [productToShow.pDesc sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping];

警告 sizeWithFont:constrainedToSize:lineBreakMode:is first deprecated in iOS7

所以我尝试按照编译器的建议使用boundingRectWithSize:options:attributes:context:

但代码似乎更复杂:苹果不会让更简单的事情变得困难。如果有人能看到我的代码并提出最好的方法或更好、更简单的方法,那将对我和社区有很大帮助。

带有 XIB 的 iOS5 代码

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row]; 

        self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
        self.lblHidden.text = tempPointStr;
        CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font 
                               constrainedToSize:self.lblHidden.frame.size
                                   lineBreakMode:NSLineBreakByWordWrapping];

        if (size.height < 50.0f)
        {
            return 50.0f;
        }
        else
        {
            return size.height;
        }

    }
    elseif(indexPath.section == 1)
    {
    ...
    }
    else
    {
    ...
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString *tempPoint = @"ProductPointsCell";
        ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint];
        if (cell == nil) 
        {
            NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ProductPointsCell_iPad" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.showsReorderControl = NO;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor clearColor];
            cell.lblPoint.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0f];
        } 

        NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row]; 
        cell.lblPoint.text = tempPointStr;
        self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
        self.lblHidden.text = tempPointStr;
        CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font 
                               constrainedToSize:self.lblHidden.frame.size
                                   lineBreakMode:NSLineBreakByWordWrapping];

        if (size.height < 50.0f)
        {
            cell.lblPoint.frame = CGRectMake(58, 0, 945, 50.0f);
        }
        else
        {
            cell.lblPoint.frame = CGRectMake(58, 0, 945, size.height);
        }
    return cell;
    }
    elseif(indexPath.section == 1)
    {
    ...
    }
    else
    {
    ...
    }
}

其中相同的代码:

带有 storybaord 的 iOS6/7 代码

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSString *tempPointStr = (self.shortDescArray)[indexPath.row];

        self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
        self.lblHidden.text = tempPointStr;

        CGSize constraint = CGSizeMake(945, 9999.0f);
        NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName];
        NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:self.lblHidden.text attributes:attributes];
        CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGSize size = rect.size;

        if (size.height < 50.0f)
        {
            return 50.0f;
        }
        else
        {
            return size.height;
        }
    }
    elseif(indexPath.section == 1)
    {
    ...
    }
    else
    {
    ...
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString *tempPoint = @"ProductPointsCell";
        ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint];
        cell.showsReorderControl = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];
        cell.pointLbl.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0f];
        [cell.pointLbl setLineBreakMode:NSLineBreakByWordWrapping];
        [cell.pointLbl setNumberOfLines:0];

        NSString *tempPointStr = (self.shortDescArray)[indexPath.row];
        cell.pointLbl.text = tempPointStr;
        CGSize constraint = CGSizeMake(945,9999.0f);
        NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:cell.pointLbl.text attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
        CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGSize size = rect.size;

        if (size.height < 50.0f)
        {
            cell.pointLbl.frame = CGRectMake(58, 0, 945, 50.0f);

        }
        else
        {
            cell.pointLbl.frame = CGRectMake(58, 0, 945, size.height);
        }
        return cell;
    }
    elseif(indexPath.section == 1)
    {
    ...
    }
    else
    {
    ...
    }
}

这些代码的目的是使数据在适合单行时显示在单行中,当不适合时调整单元格高度并将数据显示为多行单元格。

如果我应该保持自动布局打开或关闭,以及我是否应该在转换时在 Storyboard 中设置任何参数,请同时纠正我。

如果有人可以提出更好/更快和更简单的方法,请举例说明。

最佳答案

在没有 NSAttributedString 的情况下尝试...这对我有用:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSManagedObject *obj = [results objectAtIndex:indexPath.row];
    NSString *text = [obj valueForKey:@"name"];

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil];


   CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

   CGSize size = [text boundingRectWithSize:constraint  options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;


    CGFloat height = MAX(size.height, 45.0f);


    return height ;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";


    TVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[TVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    NSManagedObject *obj = [results objectAtIndex:indexPath.row];
    NSString *text = [obj valueForKey:@"name"];

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;

    [cell.cellText setText:text];
    [cell.cellText setFrame:CGRectMake(CELL_CONTENT_MARGIN, 0.0, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 45.0f))];

    return cell;
}

在 TVCell.m 中:

    self.cellText = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.cellText setLineBreakMode:NSLineBreakByWordWrapping];
    [self.cellText setMinimumScaleFactor:FONT_SIZE];
    [self.cellText setNumberOfLines:0];
    [self.cellText setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    self.cellText.textColor = [UIColor colorWithRed:64/255.0 green:59/255.0 blue:59/255.0 alpha:1.0];
    [self.contentView addSubview:self.cellText];

关于ios - 单元格高度和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19126245/

相关文章:

ios - UITableView 自定义单元格在滚动时复制 UIButton

swift - 桌面 View 的自动高度

ios - 缓存图像和 UICollectionView

ios - UIPopoverController 顶部的 UITableViewController 不可见

ios - 'RefreshControl' 上的“数组索引超出范围”

java - 内容出现在自定义单元格工厂的空行中

ios - 如何将数据保存到一个 View Controller 中的 CoreData 中,然后让 tableview 在另一个 View Controller 中显示数据?

iphone - 如何使用 Storyboard更改 iOS5 中的初始 View ?

iphone - 为回合制多人 iPhone 游戏构建我自己的游戏服务器

ios - UISplitViewController 弹出菜单填满屏幕,如何更改大小?