How to remove cell from static UITableView created in Storyboard

前端 未结 7 2399
野性不改
野性不改 2021-02-14 08:11

This should be easy, but I\'m having trouble.

I have a static UITableView with a cell that I would like to remove programmatically if it\'s not needed.

I have a

7条回答
  •  悲哀的现实
    2021-02-14 08:29

    Set the cell you want to hide to hidden somewhere in your code. Add this code: (If your cell has different row height, then you need to override more functions)

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        int rowCount=0;
        for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){
            NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section];
            UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
            if (!cell.hidden){
                ++rowCount;
            }
        }
        return rowCount;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        int realRow=-1;
        for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){
            NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section];
            UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
            if (!cell.hidden){
                ++realRow;
            }
            if (realRow==indexPath.row)
                return cell;
        }
        return nil;
    }
    

提交回复
热议问题