Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide UITableview cell

i'm trying to Hide a cell from a UITableView. just like the delete action do but i just want to hide it to later show it in the same position.

i know that UITableViewCell has a property called "Hidden" but when i hide the Cell using this property it hide but no animated and they leave a blank space

Example:

  1. first cell
  2. second cell
  3. third cell

it's possible that when i hide the second cell, that third cell change position to 2 ?

thanks

like image 442
user1628700 Avatar asked Apr 27 '15 03:04

user1628700


People also ask

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.


2 Answers

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.

like image 179
Tim Johnsen Avatar answered Sep 20 '22 17:09

Tim Johnsen


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

like image 45
Mohammad Zaid Pathan Avatar answered Sep 19 '22 17:09

Mohammad Zaid Pathan