2017-10-16 87 views
0

我有一個有趣的場景。我有一些在單獨的xib文件中設計的自定義UITableViewCells。其中之一有一個UIImageView,它將加載大小不恆定的圖像,所以這意味着UIImageView的高度必須靈活。這個單元格還有一個包含UILabels的UIView,並且UIView的大小是不變的,比如100。我想展開並摺疊didselectrowatindexpath事件上的單元格。要摺疊單元格,我必須隱藏有一些標籤的UIView。請在這方面指導我達到目標。 另外我的問題是「如何計算細胞展開和摺疊時行的高度。」謝謝如何計算自定義展開/摺疊UITableViewCell的高度動態

編輯:這是我所嘗試過的。 。 。但失敗

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
 
    static DynamicTableViewCell *cell = nil; 
 
    static dispatch_once_t onceToken; 
 

 
    dispatch_once(&onceToken, ^{ 
 
     cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
 
    }); 
 
    
 
    [self setUpCell:cell atIndexPath:indexPath]; 
 
    
 
    CGFloat cellHeight = [self calculateHeightForConfiguredSizingCell:cell]; 
 
    if(cellHeight >0) { 
 
     if(cell.isExpanded) { 
 
      
 
      return cellHeight; 
 
     } 
 
     else 
 
     { 
 
      return (cellHeight - detailViewHeight);  // cell.detailView.frame.size.height = 100 
 
     } 
 

 
    } 
 
    else { 
 
     return cellHeight; 
 
    } 
 
}

+0

向我們展示一些代碼。你試過什麼了? – onnoweb

回答

0

首先你不應該有,因爲性能原因,任何參考實例,以你的。

其次,您應該使用模型以正確的方式構建您的單元格。所提供的代碼根本不顯示模型存儲的使用情況。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Say you have a array of models NSArray<YourModel *> *dataSource; 

    YourModel *model = dataSource[indexPath.row]; // hope you have only one section. 
} 

三是使用像MVVM或VIPER,或任何MVC架構的好方法,因爲如果你沒有一個你可能會在你的產品的未來支持問題。所以在MVVM的情況下YourModel就像ViewModel。

要定義動態高度單元格的狀態,請使用屬性isExpanded。這是一個好點,但它應該在另一個地方定義 - YourModel。如果你能以正確的方式做到這一點,那麼實際上你就會知道沒有細胞的細胞狀態。

@interface YourModel ... 
@property BOOL isExpanded; 
@end 

要確保你正確地改變你的狀態didSelect:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    YourModel *model = dataSource[indexPath.row]; 
    model.isExpanded = !model.isExpanded; // inverse works like switch 
    // Then relayout your row 
    [tableView beginUpdates]; 
    // I commented next line because it might not be needed for you 
    // Test it please 
    // [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    [tableView endUpdates]; 
} 

所以回到heightForRow:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Say you have a array of models NSArray<YourModel *> *dataSource; 

    YourModel *model = dataSource[indexPath.row]; // hope you have only one section. 

    // It's better to extract next code to function or any class, but not in your YourModel. 
    CGSize imageSize = [model.image doSizeCalulation]; // Use your algorithm to calculate image size 
    CGSize shortText = [model.shortText sizeWithAttributes:@[/* font attributes */]]; 

    // If the cell is expanded then we should calculate height of text else height is zero. 
    CGSize fullText = model.isExpanded ? [model.fullText sizeWithAttributes:@[/* font attributes */]]; : 0; 

    // just sum 
    return imageSize.height + shortText.height + fullText.height; 
} 

另一種方式來實現這一目標用UITableViewAutomaticDimension(只返回它heightForRow方法)。在這種情況下,您應該正確設置約束,並在運行時根據isExpanded屬性更改fullText的高度約束常量。

+0

嗨亞歷山大Shitikov,我謝謝你從我的內心深處這樣一個詳細的答案。我是iOS新手,你的這種詳細回覆真的幫助我。還有一些小問題需要回答,我想改變單元格的高度,以便在didselectrowatindexdexpath上做出摺疊/展開效果。請在這方面指導我。 –

+0

我也無法理解,只有我的單元格引用了imageView和IBOutlets的標籤。那麼如何實現CGSize imageSize = [model.image doSizeCalulation];接下來,您還說明了isExpanded屬性應該在模型中,我的問題是「那麼我將如何在didselectrowatindexdex函數中更新它,請您提供一些示例代碼。我會很感激。 –

+0

你好!請再看看答案,有一個代碼塊與didSelectRow的例子,它清楚地說明你應該如何更新isExpanded屬性 –