[英]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.