我有一个使用IB / Storyboard中的静态单元格创建的表格视图表单。但是,我需要根据某些条件在运行时隐藏某些单元格。
我找到了一些答案。关于这个问题,例如
UITableView设置为静态单元格。是否可以以编程方式隐藏某些单元格?
..他们专注于将单元格/行的高度设置为0。这很好,除了我现在从AutoLayout中获得了例外,因为无法满足约束。我如何解决最后一个问题?我可以暂时禁用子视图的自动版式吗?在iOS7中是否有更好的方法可以做到这一点?
我发现执行此操作的最佳方法是简单地处理numberOfRowsInSection,cellForRowAtIndexPath和heightForRowAtIndexPath以选择性地删除某些行。这是针对我的方案的“硬编码”示例,您可以做一些更聪明的事情来智能地删除某些单元,而不是像这样对它们进行硬编码,但这对于我的简单方案而言最简单。
- (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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句