Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView set tableview row hidden

I have a custom tableview cell in grouptableview. And I have one hidden. I then have to make it visible. Cell tag is 3.

This is not working my code:

if (self.tableView.tag == 3) {                 self.tableView.hidden = NO; //Not working.             } 

Just i need make a one row is visible. I hope you understand.

like image 960
Salieh Avatar asked May 05 '13 17:05

Salieh


2 Answers

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 you cell.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {     let myCell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellID",for: indexPath) as! UITableViewCell      if(indexPath.row < 2){         myCell.isHidden = true     }else{         myCell.isHidden = false     }      return myCell } 
  2. Set Height of cell to ZERO.

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {     var rowHeight:CGFloat = 0.0      if(indexPath.row < 2){         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.

like image 67
Mohammad Zaid Pathan Avatar answered Sep 30 '22 12:09

Mohammad Zaid Pathan


Pass the cell height zero for that specific cell in the heightForRowAtIndexPath: , it will automatically get hidden:-

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath   {       float heightForRow = 40;        YourCustomCell *cell =(YourCustomCell *)[tableView cellForRowAtIndexPath:indexPath];        if(cell.tag==3)           return 0;       else            return heightForRow;  } 

Add the following method to your code , it will do the trick . Hope it will help you .

like image 34
Gaurav Rastogi Avatar answered Sep 30 '22 12:09

Gaurav Rastogi