ios – 在具有静态单元格的UITableView中隐藏单元格,并且没有aut
发布时间:2020-12-15 01:56:04 所属栏目:百科 来源:网络整理
导读:我有一个在IB /故事板中使用静态单元创建的表格视图。但是,根据某些条件,我需要在运行时隐藏一些单元格。 我找到了几个答案;关于这个问题,例如 UITableView set to static cells. Is it possible to hide some of the cells programmatically? ..他们专注
我有一个在IB /故事板中使用静态单元创建的表格视图。但是,根据某些条件,我需要在运行时隐藏一些单元格。
我找到了几个答案;关于这个问题,例如 UITableView set to static cells. Is it possible to hide some of the cells programmatically? ..他们专注于将单元格/行的高度设置为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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |