我想弄清楚如何优化性能和提高代码质量。 我有以下布局:嵌套的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指向一张桌子吗? –