使用静态单元隐藏UITableView中的单元格,并且不会自动布局崩溃

我有一个使用IB / Storyboard中的Static Cells创build的表格视图表单。 但是,我需要在运行时根据特定条件隐藏一些单元。

我find了一些答案。 对于这个问题,例如

UITableView设置为静态单元格。 是否有可能以编程方式隐藏一些单元格?

..他们专注于设置单元格/行的高度为0.这是伟大的,除了我现在从AutoLayout获取exception,因为约束不能满足。 我如何解决这个最后的问题? 我可以暂时禁用子视图的自动布局吗? 有没有更好的方法在iOS7中做到这一点?

我发现最好的办法是简单地处理numberOfRowsInSection,cellForRowAtIndexPath和heightForRowAtIndexPath来select性地删除特定的行。 下面是我的场景的一个“硬编码”示例,您可以做一些更智能的事情,智能地删除某些单元格,而不是像这样硬编码,但这对于我的简单场景来说是最简单的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; if (indexPath.section == 0 && hideStuff) { cell = self.cellIWantToShow; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath]; if (indexPath.section == 0 && hideStuff) { height = [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]]; } return height; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger count = [super tableView:tableView numberOfRowsInSection:section]; if (section == 0 && hideStuff) { count -= hiddenCells.count; } return count; } 

隐藏故事板上的单元格并将高度设置为0:

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { let cell: UITableViewCell = super.tableView(tableView, cellForRowAtIndexPath:indexPath) return cell.hidden ? 0 : super.tableView(tableView, heightForRowAtIndexPath:indexPath) } } 

最简单的方法是更改​​部分和行的高度。 这个对我有用。

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 3) { return 0; } else { return [super tableView:tableView heightForHeaderInSection:section]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; if (indexPath.section == 3) { cell.hidden = YES; return 0; } else { cell.hidden = NO; return [super tableView:tableView heightForRowAtIndexPath:indexPath]; } } 

洁净的方法是使用dynamic单元格,将行高设置为0是一种破解。 静态单元非常方便,但function有限。

如果确保触摸单元格底边没有任何限制,则自动布局不应该在iOS 6.0,6.1,7.0上testing。 你仍然会“锚定”到顶部边缘,并且必须钉住高度。 (当然,你可以做相反的事情,并把它锚定在底部)。

如果您的布局取决于顶部和底部的边缘位置,则可以通过编程方式删除约束(毕竟,它们只是对象)。

我设法通过以编程方式在viewDidLoad删除单元格contentView的约束,然后在heightForRowAtIndexPath中将单元格的高度设置为0,从而避免了自动布局的exception。

我已经find了一种方法,甚至可以排行animation,并在iOS 8.3上工作。 所有你需要的是实现tableView:numberOfRowsInSection:数据源方法,然后通过UITableView方法添加/删除行insertRowsAtIndexPaths:withRowAnimation:deleteRowsAtIndexPaths:withRowAnimation:。

以下是基于UISwitch状态隐藏精确行的示例:

 - (IBAction)alowNotif:(id)sender { UISwitch *sw = (UISwitch*)sender; NSIndexPath *index = [NSIndexPath indexPathForRow:5 inSection:0]; if ([sw isOn]) { [self.tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic]; } else { [self.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic]; } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (![notifications isOn]) { return 5; } return 6; } 

正如上面提到的@algal, numberOfRowInSection:仍然是UITableViewDataSource方法,所以不会简单地知道它的工作时间。

对我来说最好的办法是修改numberOfRowsInSection方法。 我删除了我不想显示的数据源。 最好的解决scheme,因为一切都在一个function。

 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section==0) { NSUInteger productCount = _products.count; for(loop trough your data) { if(your condition is true) productCount--; } } return productCount; } else return self.groups.count; } 

对于StoryBoard中的tableview,也实现了这一点。 我的单元格embedded在一个标题的部分,由xcode6B中的蓝色立方体表示。 事实上,如果你实现了heightForHeaderInSection,heightForFooterInSection和titleForHeaderInSection,titleForFooterInSection,你可以在表显示的时候访问头文件,分别返回0.0和nil,并且为numberOfRowsInSection返回0。

基本上这一切工作正常,除了每个单元格隐藏ca. 每个隐藏的单元(部分)都保留10像素高的垂直空间。 任何想法可能是什么?