2014-02-24 40 views
0

我想弄清楚如何優化性能和提高代碼質量。 我有以下佈局:嵌套的UITableVIew heightForRowAtIndexPath

OuterTableView 
Outer Cell 
    UILabel (title label) 
    InnerTableView 
    Inner Cell (item cell) 
    Inner Cell (item cell) 
    ... 

此佈局的目的是可擴張的外表。在摺疊狀態下,只有UILabel(只有標題標籤)可見。當用戶單擊外部單元格時,單元格會展開並顯示內部單元格。

外臺控制器我有3個功能(簡化):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.tableView) { 
     // ServiceGroupCell - outer cell 
     ServiceGroupCell *serviceCell = [tableView dequeueReusableCellWithIdentifier:@"service_group_cell"]; 
     [self configureCell:serviceCell forIndexPath:indexPath]; 
     return serviceCell; 
    } 

    return nil; 
} 

- (void) configureCell:(ServiceGroupCell *) cell forIndexPath:(NSIndexPath *) indexPath { 
    // styling for outer cell and passing data for inner cells 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ServiceGroupCell *serviceGroupCell = [tableView dequeueReusableCellWithIdentifier:@"service_group_cell"]; 
    [self configureCell:serviceGroupCell forIndexPath:indexPath]; 
    [serviceGroupCell layoutSubviews]; 

    CGFloat height = [serviceGroupCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 

    return height; 
} 

內表控制器

- (void)layoutSubviews { 
    [self.cellTableView layoutSubviews]; 
    // set height constraint for inner table view 
    self.heightLayoutConstraint.constant = self.cellTableView.contentSize.height; 
} 

我有疑問: 1)正如你可以看到每個外部表configureCell的單元被調用兩次。有什麼方法緩存或只調用一次? 2)systemLayoutSizeFittingSize返回0.是否有任何想法爲什麼?

+0

你想讓configureView被調用一次嗎?我瞭解你的問題嗎? –

+0

@Basheer_CAD是的,我想調用一次configureCell。 – user2786037

+0

你確定self.tableView指向一張桌子嗎? –

回答

-1

細胞內其他細胞是一個壞主意,我想。您可以創建一些部分,並使用方法didSelectRowAtIndexPath,當用戶點擊部分中的單元格時,將單元格添加到此部分。或使用滾動視圖與其中放tableview的自定義nsview。這只是解決。

有什麼方法可以緩存或只調用一次嗎? - 嘗試willDisplayCell方法。

0

2)systemLayoutSizeFittingSize返回0.是否有任何想法爲什麼?

您的約束可能會丟失。確保在.xib文件中指定單元格寬度和高度的約束。