2015-07-21 68 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建築&運行