【问题标题】:UITableView button and label is getting removed when i scroll up and down当我向上和向下滚动时,UITableView 按钮和标签被删除
【发布时间】:2018-07-25 06:50:30
【问题描述】:

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

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

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

当我上下滚动时,已经显示单元格按钮、标签和所有再次隐藏。请帮帮我。

提前致谢!

【问题讨论】:

  • cellForRowAtIndexPath() 每次单元格移出滚动视图并返回时都会被调用..
  • @Skywalker 所以我需要做什么来处理这个
  • 处理它的一种方法是创建structclass 并添加属性selected 并在单击单元格时更新它并从cellForRowAt 方法中读取。
  • @DharmeshKheni 任何示例代码都会有所帮助
  • 如果你在谷歌上试一试,你会找到一个例子

标签: ios swift uitableview


【解决方案1】:

单击按钮时,您需要将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 可能会帮助您了解工作流程。 showHidenoutlets 可以作为调用控制器的委托方法。

【讨论】:

  • 如果我将showHidenoutlets 调用到我的按钮操作,那么它会抛出Use of undeclared type 'DetailsCell' 的错误
  • 您需要在按钮操作中调用它,您不能将其分配给按钮操作。
  • 是的,但是我怎么称呼它...我尝试了按钮操作仍然出现“DetailsCell”错误
  • 我添加了一个教程链接。看起来你有比这更大的问题。
猜你喜欢
  • 2020-04-06
  • 2017-04-04
  • 2021-10-09
  • 2015-06-07
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
相关资源
最近更新 更多