简体   繁体   中英

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

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.

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

You can't simply hide a UITableViewCell. You have to remove that cell from the table view then insert it again when you would like it to reappear in the table.

The methods you're looking for are deleteRowsAtIndexPaths:withRowAnimation: and insertRowsAtIndexPaths:withRowAnimation: . These are well documented in the UITableView documentation here . You're going to have to remember where you removed the cell from then insert it at the same position later.

Keep in mind that if you add cells to your table with a lesser row index than the row index of the deleted row, you will have to add on to the index to maintain it's relative position.

Hope this helps.

Same as Zaid Pathan but for Swift 4:

    //HIDE you cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {

    let myCell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! UITableViewCell

    //hide second cell
    indexPath.row == 1 ? (cell.isHidden = true): (cell.isHidden = false)

    return myCell       
}


//Set Height of cell to ZERO.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    var rowHeight:CGFloat = 0.0
    indexPath.row == 1 ? (rowHeight = 0.0): (rowHeight = 49.0)
return rowHeight
}

I am using Hidden in Attributes Inspector of TableViewCell -> ContentView and then implement method:

   override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if shouldHideSection(indexPath.section) {
        return 0.0
    } else if let isHidden = tableView.cellForRow(at: indexPath)?.contentView.isHidden, isHidden {
        return 0.0
    }

    return super.tableView(tableView, heightForRowAt: indexPath)
}

If you want the other cells to have a dynamic height:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 1 { // Or any other row
            return 0
        }
        return -1.0
    }

您应该删除行并重新加载表视图 [tbv reloadData];

cell display, depending on the data source, so you need to deal with the data source.

if dataArr is the table view datasource,first of all, you should delete the data source where the cell,then

[dataArr removeObjectAtIndex:indexpath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

at last ,you can insert data source when you should add a cell

[dataArr insertObjectAtIndex:indexpath.row];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

Best way to set rowHeight value in condition check

Dataarray where value stored .

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //set up here
    
     let cell = myTableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)  as! myCustomCell
    
    
    if (dataArray?[indexPath.row].somevalue == "true")
    {
        cell.isHidden = true
        tableview.rowHeight = 0.0
      
    }
    else{
    // show cell values 
     tableview.rowHeight = 130
       

} `

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

             let dicttemp : Dictionary = arrAllCoursesList[indexPath.row] as! Dictionary<String,Any>

             var rowHeight:CGFloat = 0.0

             if let getStatus = dicttemp["status"] as? String
             {
                if getStatus == "1"
                {
                    rowHeight = 240.0
                }
                else
                {
                     rowHeight = 0.0
                }
             }

            return rowHeight

        }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

             let cell =  tableView.dequeueReusableCell(withIdentifier: "CompetitionTableViewCell") as! CompetitionTableViewCell

            let dicttemp : Dictionary = arrAllCoursesList[indexPath.row] as! Dictionary<String,Any>

  if let getStatus = dicttemp["status"] as? String
            {
                if getStatus == "1"
                {
                    cell.isHidden = false
                }
                else
                {
                    cell.isHidden = true
                }
            }


            cell.selectionStyle = UITableViewCellSelectionStyle.none
            return cell

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM