简体   繁体   中英

UITableView button and label is getting removed when i scroll up and down

I have a tableView, each cell contains 3 buttons and 3 labels. When app loads first time, 2 buttons and 2 labels will be disabled. When 3rd button is clicked, all my 2 buttons and 2 labels have to show in the cell. So each cell has to apply the same scenario. So multiple cell may contain this. But now if same process if followed for any 3 or 4 cell, even for 1 cell and scrolling up and down.Then that 2 buttons and 2 labels are hidden.

Here my code how i tried :

var selectedIndexPaths = NSMutableSet()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailsCell", for: indexPath) as! DetailsCell
if selectedIndexPaths.contains(indexPath) {
            cell.OuterViewLabel.isHidden = false
            cell.Datalabel.isHidden = false
            cell.NameButnOutlet.isHidden = false
            cell.dropButnOutlet.isHidden = false
        } else {
            cell.OuterViewLabel.isHidden = true
            cell.Datalabel.isHidden = true
            cell.NameButnOutlet.isHidden = true
            cell.dropButnOutlet.isHidden = true
        }
}

Here when i press any cell BUTTON to show my 2 button and 2 label to show that cell:

func showHidenoutlets() {    
 cell.OuterViewLabel.isHidden = false
 cell.Datalabel.isHidden = false
 cell.NameButnOutlet.isHidden = false
 cell.dropButnOutlet.isHidden = false
}

When I scroll up and down, already showing cell button, label and all again hiding. Please help me out.

Thanks in advance !

You need to add indexPath to selectedIndexPaths when button is clicked.

var selectedIndexPaths = NSMutableSet()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailsCell", for: indexPath) as! DetailsCell
    let notClicked = !selectedIndexPaths.contains(indexPath)
    cell.OuterViewLabel.isHidden = notClicked
    cell.Datalabel.isHidden = notClicked
    cell.NameButnOutlet.isHidden = notClicked
    cell.dropButnOutlet.isHidden = notClicked
}

func showHidenoutlets(cell: DetailsCell, indexPath: IndexPath, add: Bool = true) {
    if add {
        selectedIndexPaths.add(indexPath)
    }
    cell.OuterViewLabel.isHidden = !add
    cell.Datalabel.isHidden = !add
    cell.NameButnOutlet.isHidden = !add
    cell.dropButnOutlet.isHidden = !add
}

This might help you understand work flow. showHidenoutlets can be delegate method to call on controller.

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