2015-07-21 32 views
6

我正在面对使用自动布局自动调整尺寸单元的问题。我的目标是取得一个表,这将是这个样子:IOS - 自我尺寸单元问题

| 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(因为标题与它们重叠)。

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

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

任何想法?

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

我试图

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

告诉标题标签是什么它的最大宽度,但它搅乱的事情了。

回答

2

和你一样,我已经设置标题和描述的行为0,我已经设置了你的是这样的:

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

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

标题:

  • 抱死:H:251,V:25​​2
  • 压缩:H:999,五:1000

时间:

  • 抱死:H:253,V:25​​1
  • 压缩:H:1000,V:750

价格:

  • Hugg荷兰国际集团:H:252,V:25​​1
  • 压缩:H:1000,V:750

描述:

  • 抱死:H:251,V:25​​1
  • 压缩:H:750 V:999

一切正常

+0

感谢您的回答,不幸的是它不适用于我(没有layoutIfNeeded())。添加layoutIfNeeded()标题标签后,我收到了很多NSConstraints错误。描述仍然被截断。 – Cahir09

+0

我创建了新的测试项目并按照您的建议设置了所有约束,压缩和拥抱属性。但标题仍被截断。如果在返回单元格之前添加layoutIfNeeded,它看起来很好,但是存在一堆布局约束错误。好消息是显示整个描述。在我的项目中,数据是从WS加载的,使用适当的数据填充单元有点复杂。它可能是描述被切断的原因吗?我的意思是细胞在显示之前无法计算其高度?如果你仍然有你的示例项目,你可以上传并分享链接? – Cahir09

+0

我只在IB上试过它,但它工作正常。一旦有空闲时间,我会安排一个项目。 – Daniel

2

enter image description here您可以首先将约束添加到价格中,作为领先,顶部,高度,宽度。然后将时间标记为顶部,尾部,高度,宽度。标题标签为前导,尾随,顶部,底部。描述标签的引导,尾随,顶部,底部。 &写下面的代码。 heightForRowAtIndexPath会给你适当的高度,以你的

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static YOUR_TABLEVIEW_CELL *sizingCell = nil; 
    static NSString *[email protected]"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 
    [email protected]"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 *[email protected]"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; 
} 
2

,请返回您配置的电池之前添加

[cell layoutIfNeeded] 

0
  1. 最小宽度添加到时间&价格拉布勒

  2. 设置拥抱优先太

标题标签:

抱死:H:1000,V:1000

压缩:H:1000,V:1000

描述标签:

抱死:H:1000,V:999

压缩:H:1000,V:999

  • 添加此行在的cellForRowAtIndexPath的端&除去layoutSubviews

    细胞?.layoutIfNeeded()

  • 添加本功能

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

  • FUNC的tableView(的tableView:UITableView的,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat的{ 回报UITableViewAutomaticDimension }

    5建筑&运行