我想弄清楚如何優化性能和提高代碼質量。 我有以下佈局:嵌套的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.是否有任何想法爲什麼?
你想讓configureView被調用一次嗎?我瞭解你的問題嗎? –
@Basheer_CAD是的,我想調用一次configureCell。 – user2786037
你確定self.tableView指向一張桌子嗎? –