【问题标题】:IOS - self sizing cells issueIOS - 自定尺寸单元问题
【发布时间】:2015-07-21 08:58:51
【问题描述】:

我遇到了使用自动布局自动调整单元格大小的问题。我的目标是实现一个看起来像这样的表格:

| Title_label (time)      $price |
|                                |
|Some long description. More     |
|description.                    |
|                                |

如果标题很长,它应该是这样的:

| This title is (time)    $price |
| really long                    |
|                                |
|Some long description. More     |
|description.                    |
|                                |

所以当标题变大时,只要在时间和价格之间有 8 个点的空间,它就会将时间标签向右推。如果它更大,它应该换到下一行。

我在表格视图之前使用了自调整大小的单元格,但那里只有一个扩展标签,而不是两个。

我已经实现了行的自动高度:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100

这就是我的约束的样子:

|     8px
|8px title 8px time >=8px       price|
|     8px                            |
|8px description                  8px|
|     8px                            |

价格时间和标题之间也有顶部对齐。

我已将标题和描述的行数设置为 0。 我已将时间和价格的压缩阻力设置为 1000(因为标题与它们重叠)。

但是标题标签不会换行到下一行。它以....结尾。更多的是描述标签也太小了。当我滚动表格时,描述的高度是固定的。

我尝试在返回单元格之前添加 cell.layoutIfNeeded()。然后单元格布局变得混乱(标题被剪裁)但是当我滚动电视时一切正常。

有什么想法吗?

编辑: 这是因为标题标签在其他标签旁边,它不知道什么时候应该换行?

我试过了

override func layoutSubviews() {
        self.nameLabel.preferredMaxLayoutWidth -= (durationLabel.frame.width + priceLabel.frame.width + 16)
        super.layoutSubviews()
    }

告诉标题标签它的最大宽度是多少,但它搞砸了。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    就像你一样,我已经将标题和描述的行设置为 0,并且我已经这样设置了你的单元格:

    |     8px
    |8px title 8px time >=8px       price|
    |     8px                            |
    |8px description                  8px|
    |     8px                            |
    

    然后,我设置了压缩和拥抱属性:

    标题:

    • 拥抱:H:251, V:252
    • 压缩:H:999,V:1000

    时间:

    • 拥抱:H:253, V:251
    • 压缩:H:1000, V:750

    价格:

    • 拥抱:H:252, V:251
    • 压缩:H:1000, V:750

    说明:

    • 拥抱:H:251, V:251
    • 压缩:H:750, V:999

    一切都按预期进行

    【讨论】:

    • 感谢您的回答,不幸的是它对我不起作用(没有 layoutIfNeeded())。添加 layoutIfNeeded() 标题标签后,我得到了很多 NSConstraints 错误。并且描述仍然被截断。
    • 我创建了新的测试项目并按照您的建议设置了所有约束、压缩和拥抱属性。但是标题仍然被截断。如果我在返回单元格之前添加 layoutIfNeeded 它看起来不错,但有一堆布局约束错误。好消息是显示了整个描述。在我的项目中,数据是从 WS 加载的,用适当的数据填充单元格有点复杂。这可能是描述被切断的原因吗?我的意思是单元格在显示之前无法计算其高度?如果您还有示例项目,能否上传并分享链接?
    • 我只在 IB 上尝试过,但它确实有效。有空我会设置一个项目。
    【解决方案2】:

    您可以先将约束添加到价格,如前导、顶部、高度、宽度。然后将时间标签设为顶部、尾随、高度、宽度。标题标签为前导、尾随、顶部、底部。用于描述标签前导、尾随、顶部、底部。 & 编写以下代码。 heightForRowAtIndexPath 将为您的单元格提供适当的高度

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static YOUR_TABLEVIEW_CELL *sizingCell = nil;
        static NSString *CellIdentifier=@"YOUR_TABLEVIEW_CELL_IDENTIFIER";
        sizingCell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (sizingCell==nil)
        {
            sizingCell=[[YOUR_TABLEVIEW_CELL alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        [self configureFareIssueCell:sizingCell atIndexPath:indexPath];
        return [self calculateHeightForConfiguredSizingCell:sizingCell];
    }
    
    //assign all the labels  here
    - (void)configureFareIssueCell:(YOUR_TABLEVIEW_CELL* )cell atIndexPath:(NSIndexPath *)indexPath
    {
        //e.g 
        cell.lbl.text=@"YOUR_TEXT";
        cell.imageView.image=[UIImage imageNamed:@"NAME_OF_YOUR_IMAGE"];
    } 
    
    - (CGFloat)calculateHeightForConfiguredSizingCell:(YOUR_TABLEVIEW_CELL *)sizingCell
    {
        CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        return size.height + 1.0f; // Add 1.0f for the cell separator height
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       static NSString *CellIdentifier=@"YOUR_TABLEVIEW_CELL_IDENTIFIER";
       YOUR_TABLEVIEW_CELL   *cell =[tableView dequeueReusableCellWithIdentifier:@"YOUR_TABLEVIEW_CELL_IDENTIFIER"];
        if (cell==nil)
        {
            cell=[[YOUR_TABLEVIEW_CELL alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        [self configureFareIssueCell:cell atIndexPath:indexPath];
    
       return cell;
    }  
    

    【讨论】:

      【解决方案3】:

      尝试添加

      [cell layoutIfNeeded]
      

      在返回您配置的单元格之前。

      【讨论】:

        【解决方案4】:
        1. 在时间和价格标签中添加最小宽度

        2. 也要设置拥抱的优先级

        标题标签:

        拥抱:H:1000,V:1000

        压缩:H:1000,V:1000

        描述标签:

        拥抱:H:1000,V:999

        压缩:H:1000,V:999

        1. 在单元格末尾添加这一行forRowAtIndexPath & 删除 layoutSubviews

          单元格?.layoutIfNeeded()

        2. 添加此功能

          func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 返回 UITableViewAutomaticDimension }

        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 返回 UITableViewAutomaticDimension }

        5 构建和运行

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-24
          • 2019-12-04
          • 2015-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多