繁体   English   中英

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

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

我有一个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
}

当我上下滚动时,已经显示了单元格按钮,标签,并且全部再次隐藏。 请帮帮我。

提前致谢 !

单击按钮时,需要将indexPath添加到selectedIndexPaths

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
}

可以帮助您了解工作流程。 showHidenoutlets可以是在控制器上调用的委托方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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