UITableView establece la fila de vista de tabla oculta

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.

preguntado el 05 de mayo de 13 a las 18:05

You are hiding your entire tableView there, not a row. -

4 Respuestas

In SWIFT you need to do two things,

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

  2. Set Height of cell to CERO.

Mira aquí,

  1. ESCONDER 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 CERO.

    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.

Puedes hacer lo mismo por cell?.tag also to hide specific cell by tag.

Respondido el 08 de diciembre de 16 a las 09:12

when i use this i get a warning: "Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it" - DrPaciencia

I don't understand why the need to hide the cell? How does setting the height to zero cause reusable cells to conflict? - bradley thomas

This is actually the best answer for swift and non-swift. For me, I needed to hide the cell as well as setting the height to 0. If not hidden, I see part of the cell even though the height is zero. In my case, I have a background image on the cell - jeffb6688

Also, remember to add the 'override' keyword infant of the function. It will then read: override func tableView(....) - Native_Mobile_Arch_Dev

I'm also getting constraint warming issues. Anybody figure this out? - user7097242

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 .

contestado el 05 de mayo de 13 a las 18:05

This worked for me. I did change my code to float heightForRow = tableView.rowHeight; That property returns the default height if it is unset. - erik allen

This does not work since cellForRowAtIndexPath: causes it to crash, because the cells do not exist yet. The height is set up before the cells are created. - jordan h

According to me this is not a good solution, because when cell's are reused it may cause conflict. - Mohammad Zaid Pathan

when i use this i get a warning: "Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it" - DrPaciencia

I recommend Zaid Pathan's answer, even for non-swift implementations. - jeffb6688

please refer this code :-

 - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
 {
  if(section == theSectionWithoutARow)
{
    if(shouldRemoveTheRow)
        return [theArrayWithTheSectionContents count] - 1;
    else
        return [theArrayWithTheSectionContents count];
  }
   // other sections, whatever
  }

    - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:          (NSIndexPath *)indexPath
     {
       // blah blah standard table cell creation

    id theCellObject;

     if(indexPath.section == theSectionWithoutARow)
     {
    NSInteger theActualRowToDisplay = indexPath.row;
    if(shouldRemoveTheRow && indexPath.row >= theRowIndexToRemove)

    {
        theActualRowToDisplay = indexPath.row + 1;
    }
    theCellObject = [theArrayWithTheSectionContents objectAtIndex:theActualRowToDisplay];
}

// now set up the cell with theCellObject

return cell;
  }

Espero que esto te ayude

contestado el 05 de mayo de 13 a las 18:05

Images[![] [1] enter image description here

Here is my scenario. First of all, my table view is static. And in section "Account", there should alway be only one cell displayed. LoggedInCell is displayed when user is logged in, and unLoggedInCell when user is not logged in. One solution is to set their height zero, but you may encounter NSContraints error which is complex to be fixed. My solution is below:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let count = super.tableView(tableView, numberOfRowsInSection: section)
    if section == 0 {
        return count - 1
    }
    return count
}

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

    if indexPath.section == 0 {
        if userForApp == nil {
            return super.tableView(tableView, cellForRowAt: IndexPath(row: 0, section: 0))
        } else {
            return super.tableView(tableView, cellForRowAt: IndexPath(row: 1, section: 0))
        }
    } else {
        return super.tableView(tableView, cellForRowAt: indexPath)
    }

}

quite simple! yes? Btw, You may have the cells height problem like me, I mean the two cells (UnLoggedInCell and LoggedInCell) have different height, we should tell the table view object that the value of cells height by doing this:

    var originHeightOfUnLoggedInCell: CGFloat = 0.0
    var originHeightOfLoggedInCell: CGFloat = 0.0

    func recordInitialHeightOfCells() { // called in viewDidLoad()
        self.originHeightOfUnLoggedInCell = self.unLoggedInCell.frame.height
        self.originHeightOfLoggedInCell = self.loggedInCell.frame.height
  }

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if userForApp == nil {
                return originHeightOfUnLoggedInCell
            } else {
                return originHeightOfLoggedInCell
            }
        } else {
            return super.tableView(tableView, heightForRowAt: indexPath)
        }
    }

contestado el 04 de mayo de 19 a las 17:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.