简体   繁体   English

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

[英]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. 我有一个tableView,每个单元格包含3个按钮和3个标签。 When app loads first time, 2 buttons and 2 labels will be disabled. 首次加载应用程序时,将禁用2个按钮和2个标签。 When 3rd button is clicked, all my 2 buttons and 2 labels have to show in the cell. 单击第三个按钮时,我的所有2个按钮和2个标签必须显示在单元格中。 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. 但是现在,如果对3个或4个单元格(甚至是1个单元格并向上和向下滚动)遵循相同的过程,则将隐藏2个按钮和2个标签。

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: 在这里,当我按任意单元格按钮以显示我的2按钮和2标签以显示该单元格时:

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. 单击按钮时,需要将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
}

This might help you understand work flow. 可以帮助您了解工作流程。 showHidenoutlets can be delegate method to call on controller. showHidenoutlets可以是在控制器上调用的委托方法。

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

相关问题 按钮,当我上下滚动表格视图时,标签隐藏起来 - button, label is getting hide when i scroll table view up and down 我在Swift 4中上下滚动时最喜欢的按钮更改 - Favourite button change when i scroll down and up in swift 4 为什么显示键盘时UITableVIew向下滚动而不向上滚动? - Why does the UITableVIew scroll down but not up when keyboard is shown? 向下滚动时UITableView崩溃 - UITableView crashes when scroll down 当我上下滚动UITableView时会发生什么,因为它与我要弄清楚的错误有关 - What will happen when I scroll up and down UITableView because it's related to a bug I am trying to figure it out 向上滚动时显示 header 的 uitableview,向下滚动时隐藏 header 的 uitableview - Show header of uitableview on scroll up and hide header of uitableview on scroll down 当我向下滚动很多时,带图像的UITableView会崩溃应用程序 - UITableView with images crashes app when I scroll down a lot UITableView仅在向上滚动时更新,而不是向下滚动 - UITableView only updating on scroll up, not down UITableview向上或向下滚动时崩溃以及如何为每个单元格设置水平滚动视图 - UITableview Crash when scroll up or down and how to set horizontal scroll view to every cell 向下滚动时,UITableview页脚会刷新吗? - UITableview footer getting refreshed while scroll down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM