Hide cells in a UITableView with static cells - and no autolayout crash

前端 未结 9 1786
庸人自扰
庸人自扰 2021-01-01 16:01

I have a table view form created using Static Cells in IB/Storyboard. However, I need to hide some of the cells at runtime depending on certain conditions.

I have fo

9条回答
  •  遥遥无期
    2021-01-01 16:34

    The simplest way is to change height of sections and rows. It works for me.

    - (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];
        }
    }
    

提交回复
热议问题