0

enter image description here

I have a table view cell. I make an app for a tenant in the apartment to report the defect of the room facility. if the defect has been repaired (status: Complete), data from server will give defect.status == 2 (if defect.status == 1, still on process to be repaired), and it will show YES and NO Button like the picture above.

I want if it still on the repairment process, the view that contains "Are You satisfied" label and Yes No Button will not appear. The expected result should be like the picture below

enter image description here

here is the code I use to remove that satisfied or not view

extension RequestDefectVC : UITableViewDataSource {

     //MARK: Table View Delegate & Datasource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listDefects.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "defectCell", for: indexPath) as! RequestDefectCell

        let dataDefect = listDefects[indexPath.row]
        cell.defectData = dataDefect

        if dataDefect.status == 2 {
            if let wantToRemoveView = cell.commentResponseView {
                wantToRemoveView.removeFromSuperview()
            }
        }


        return cell
    }

}

but unfortunately, if that wantToRemoveView.removeFromSuperview() is triggered, it will remove all the view in all cell, even though the status is completed like picture below

enter image description here

I want that satisfied or not view appears if the status is complete, otherwise, it will be removed. how to do that ?

Alexa289
  • 8,089
  • 10
  • 74
  • 178

4 Answers4

1

For your costumed cells are reused, removing views will cause uncertain effects. You don't actually need the specific view to be removed, only if it stays invisible.

if dataDefect.status == 2 {
    if let wantToRemoveView = cell.commentResponseView {
        wantToRemoveView.isHidden = true
    }
} else {
    if let wantToRemoveView = cell.commentResponseView {
        wantToRemoveView.isHidden = false
    }
}
LiLi Kazine
  • 195
  • 2
  • 9
0

Create a height constraint for that view and hook it as IBOutlet and control it's constant according to that in cellForRowAt

self.askViewH.constant = show ? 50 : 0

cell.layoutIfNeeded()

return cell

I expect you using automatic tableView cells

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

@Alexa289 One suggestion is that you can take heightConstraints of UIView. then create IBOutlet of your height constraints and make its constant 0 when you want to hide otherwise assign value to your static height.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "defectCell", for: indexPath) as! RequestDefectCell

    let dataDefect = listDefects[indexPath.row]
    cell.defectData = dataDefect

    if dataDefect.status == 2 {
        cell.viewSatisficationHeightConstraints.constant = 50
    } else {
        cell.viewSatisficationHeightConstraints.constant = 0
    }


    return cell
}

Second suggestion is that you can take label and button in view and embed stackview to your view(view contain label and button)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "defectCell", for: indexPath) as! RequestDefectCell

    let dataDefect = listDefects[indexPath.row]
    cell.defectData = dataDefect

    if dataDefect.status == 2 {
        cell.viewSatisfication.isHidden = false
    } else {
        cell.viewSatisfication.isHidden = true
    }


    return cell
}

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

   return UITableViewAutomaticDimension
}

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

   return 40
}
0

you can read about UIStackView which makes hiding things easier. If you are not using stackview and hiding things the UI will not good as the space used by the hidden view will be still there. So better to use stackView when need to hide or show some view.

UIStackView : https://developer.apple.com/documentation/uikit/uistackview

sanjaykmwt
  • 586
  • 3
  • 19