2014-02-24 36 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文件中指定单元格宽度和高度的约束。