当我上下滚动时,UITableView按钮和标签被删除

时间:2018-07-25 06:50:30

标签: ios swift uitableview

我有一个tableView,每个单元格包含3个按钮和3个标签。首次加载应用程序时,将禁用2个按钮和2个标签。单击第三个按钮时,我的所有2个按钮和2个标签必须显示在单元格中。因此,每个单元都必须应用相同的方案。因此,多个单元格可能包含此内容。但是现在,如果对3个或4个单元格(甚至是1个单元格并向上和向下滚动)遵循相同的过程,则将隐藏2个按钮和2个标签。

这是我尝试的代码:

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
        }
}

在这里,当我按任意单元格按钮以显示我的2按钮和2标签以显示该单元格时:

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

当我上下滚动时,已经显示了单元格按钮,标签,然后又全部隐藏了。请帮帮我。

谢谢!

1 个答案:

答案 0 :(得分:0)

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.