Hide UITableview cell

In SWIFT you need to do two things,

  1. HIDE your cell. (because reusable cell may conflict)

  2. Set Height of cell to ZERO.

Look at here,

  1. HIDE cell

    func tableView(tableView: UITableView, 
                   cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
       let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    
       if indexPath.row == 1 {
           cell?.hidden = true
       } else {
           cell?.hidden = false
       }
       return cell      
    }
    
  2. Set Height of cell to ZERO.

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
        var rowHeight:CGFloat = 0.0
    
        if(indexPath.row == 1){
            rowHeight = 0.0
        } else {
            rowHeight = 55.0    //or whatever you like
        }
        return rowHeight
    }
    

Using this you can remove reusable cell conflict issues.

You can do the same for cell?.tag also to hide specific cell by tag.

Ref: https://stackoverflow.com/a/28020367/3411787


One way to effectively "hide" a row with animation and without actually removing it is to set it's height to zero. You'd do so by overriding -tableView:heightForRowAtIndexPath:.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.0;
    if (isRowHidden) {
        height = 0.0;
    } else {
        height = 44.0;
    }
    return height;
}

(You'd only want to return 0.0 for the specific row or rows you want to hide of course, not unconditionally).

Simply changing the return value of this method doesn't make the table view automatically adjust the row's height, to make it do so you make the following calls.

isRowHidden = YES;
[tableView beginUpdates];
[tableView endUpdates];

If you do so you'll see an animated appearance/disappearance transition between the two.