【问题标题】:How to select and deselect a row by tapping on it如何通过点击来选择和取消选择一行
【发布时间】:2017-08-13 10:38:59
【问题描述】:

我有一个带有 UITableViewCell 的 UITableView,我想通过单击它来选择它们并通过再次单击它们来取消选择它们。 首先,我已经尝试过

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        // code
}

但我注意到,当您第二次单击单元格时,不会执行此功能;当您单击另一个单元格时,它会执行。 但我希望你可以点击几个单元格,当你点击它们时它们会被选中。

所以我做了这段代码(简化):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected")
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LektionTableViewCell
        if cell.accessoryType == .none {
            print("Type = none")
            cell.accessoryType = .checkmark
            Lektion.insert(Dateien[indexPath.row], at: Lektion.count)
        } else if cell.accessoryType == .checkmark {
            print("Type = check")
            cell.accessoryType = .none
            Lektion.remove(at: indexPath.row)
        }
    }

但它不能正常工作:

  1. 当我单击一个单元格时,文本会自动变为“标签”(它在视图构建器中的文本)
  2. 当我单击带有复选标记的单元格时,复选标记不会消失,Xcode 会显示“Type = none”;这是错误的。

谁能帮帮我?

【问题讨论】:

  • 代码无法工作,永远不会cellForRow之外调用dequeueReusableCell。将Bool 属性selected 添加到您的模型中,将其设置为didSelectRow 并重新加载该行。在cellForRow 中根据selected 设置附件类型。
  • @vadian 我已经尝试过了,但我不知道如何编码。你能在回答中告诉我吗?
  • Dateien的类型是什么?顺便说一句,变量名应该以小写字母开头。请在问题中添加cellForRow

标签: ios swift xcode uitableview cell


【解决方案1】:

我无法发表评论,所以我在这里添加它。 从您的代码中,我们看不到您存储用于填充单元格的数据的位置。所以我不知道这是否可以帮助你,但假设你有一个包含对象的数组,一种方法是在didSelectRowAt indexPath 方法上的对象上设置一个“isSelected”变量,然后调用tableview.reloadData()。像这样的:

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            myObjects[indexPath.row].isSelected = !myObjects[indexPath.row].isSelected 
            tableView.reloadData() 
      }

然后在cellForRowAtIndexPath 方法上,当您创建单元格时,您可以根据对象的标志轻松地将附件类型设置为 .checkmark 或 .none。 像这样:

cell.accessoryType = myObjects[indexPath.row].isSelected ? .checkMark : .none

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    从您的didSelect 方法中删除以下代码,因为它dequeReusableCell 不应在cellForRowAtIndexPath 之外使用。

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LektionTableViewCell
            if cell.accessoryType == .none {
                print("Type = none")
                cell.accessoryType = .checkmark
                Lektion.insert(Dateien[indexPath.row], at: Lektion.count)
            } else if cell.accessoryType == .checkmark {
                print("Type = check")
                cell.accessoryType = .none
                Lektion.remove(at: indexPath.row)
            }
    

    您可以在idSelectRow 中使用cellForRowAtIndexPath 获取单元格实例

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        if let cell = tableView.cellForRow(at: IndexPath) as? LektionTableViewCell {
            // Perform your operations here.....
        }
    
    }
    

    现在,您的查询的解决方案是,在单击时不会调用 didSelect(除非您选择另一行)。 允许对表格视图进行多项选择

    注意:在表格加载数据时,在表格数组中使用标志(布尔)变量来设置选择状态。

    【讨论】:

    • 但是为什么let cell = tableView.cellForRow(at: IndexPath) as? LektionTableViewCell {之前的if?
    • “let cell = ....”前不强制使用“if”,可以直接使用>>“let cell = tableView.cellForRow(at: IndexPath) as!LektionTableViewCell”为了防止异常...我通常使用多个表格和单元格相互集成...
    • 我也有同样的想法,只是'let cell = tableView.cellForRow(at: IndexPath)'
    • 然后是其他代码。结局有什么变化?
    • 在您的问题中分享函数tableview - cellForRowAtIndexPath 的代码,以便您获得更好的帮助。
    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多