How to remove cell from static UITableView created in Storyboard

前端 未结 7 2370
野性不改
野性不改 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:26

    Use index path to identify the cell in tableview height delegate and return 0

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
            if someCondition {
    
                if indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 {
                    return 0 
                }
    
            }else{
    
                if indexPath.row == 4 {
                    return 0 
                }
    
            }
    
    
        return super.tableView(tableView, heightForRowAt: indexPath)
    }
    
    0 讨论(0)
  • 2021-02-14 08:27

    You can't really deal with this in the datasource since with static tables you don't even implement the datasource methods. The height is the way to go.

    Try this:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden
            return 0.0;
        else
            return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
    } 
    

    Update

    It appears that, under autolayout, this may not be the best solution. There is an alternative answer here which may help.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-14 08:33

    It for only constant cell

    -(void)tableViewSearchPeopleCellHide:(BOOL)hide{
    
        searchCellShouldBeHidden=hide;
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        cell.hidden=hide;
        self.searchPeople.hidden=hide;//UILabel
        [self.tableView reloadData];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (searchCellShouldBeHidden) //BOOL saying cell should be hidden
            return 0.0;
        else
            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
    
    0 讨论(0)
  • 2021-02-14 08:38

    Depending on how your table is supposed to work, in your data source you can implement tableView:numberOfRowsInSection: to return 0 rows for the section based on your necessary logic.

    Updated for your comment:

    The section parameter will be populated by iOS when your implementation is called so all you need is a switch to handle the section that has the row you ant removed/hidden. Example below:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        switch(section) {
            case 0:  // first section of your table, change for your situation
                 return 0;
            default:
                 return 0;
        }
    }
    
    0 讨论(0)
  • 2021-02-14 08:45

    The first thing you can do is tag the cell from storyboard which you want to hide. Put some standard number which you can identify.

    Add this code.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
        if (cell.tag==10) { //I have put 10 for some static cell.       
                    cell.hidden=YES;
                    return 0;         
    
        }
         cell.hidden = NO;
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
    
    0 讨论(0)
提交回复
热议问题