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() }
告诉标题标签什么是它的最大宽度,但它弄乱了事情。
就像你,我已经把标题和描述的行设置为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
一切都按预期工作
您可以首先添加约束作为领导,顶部,高度,宽度的价格。 然后将时间标记为顶部,尾部,高度,宽度。 标题标签为前导,尾随,顶部,底部。 描述标签的引导,尾随,顶部,底部。 写下面的代码。 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 { //eg 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; }
尝试添加
[cell layoutIfNeeded]
在返回你的configuration单元格之前
-
添加最小宽度的时间和价格标签
-
设置拥抱的优先顺序
标题标签:
拥抱:H:1000,V:1000
压缩:H:1000,V:1000
描述标签:
拥抱:H:1000,V:999
压缩:H:1000,V:999
-
在cellforRowAtIndexPath&remove layoutSubviews的末尾添加这一行
细胞?.layoutIfNeeded()
-
添加这个function
func tableView(tableView:UITableView,estimatedHeightForRowAtIndexPath indexPath:NSIndexPath) – > CGFloat {return UITableViewAutomaticDimension}
func tableView(tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath) – > CGFloat {return UITableViewAutomaticDimension}
5构build和运行