没有在UITableViewController中调用DidSelectRowAt

时间:2018-12-14 14:56:34

标签: swift uitableview

我知道典型的解决方案是将delegate中的tableView分配给UIViewController。这里的问题是,这实际上是UITableViewController。话虽如此,这就是问题所在。

我有CategoriesTVC,它是从SwipeTVC继承的(SwipeTVC的超类是UITableViewController)。一切都很好,突然停了下来。不知道为什么。

我在didSelectRowAt方法上设置了一个断点,但从未执行过。这是代码:

class CategoryTVC: SwipeTVC {

    let realm = try! Realm()
    var categories: Results<Category>?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToItems" {
            let destination = segue.destination as! ToDoListTVC
            if let indexPath = tableView.indexPathForSelectedRow {
                destination.selectedCategory = categories?[indexPath.row]
            }
        }
    }

    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return categories?.count ?? 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = super.tableView(tableView, cellForRowAt: indexPath)
        cell.textLabel?.text = categories?[indexPath.row].name ?? "No categories added yet"
        cell.backgroundColor = UIColor(hexString: categories?[indexPath.row].backgroundColor ?? "DA7553")
        return cell
    }

    // MARK: Table View Delegate methods
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        performSegue(withIdentifier: "goToItems", sender: self)
    }

}

class SwipeTVC: UITableViewController, SwipeTableViewCellDelegate {

    var cell: UITableViewCell?

    // MARK:- Table View DataSource
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! SwipeTableViewCell
        cell.delegate = self
        return cell
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        guard orientation == .right else { return nil }
        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
            self.updateModel(at: indexPath)
        }
        deleteAction.image = UIImage(named: "delete")
        return [deleteAction]
    }

    func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
        var options = SwipeOptions()
        options.expansionStyle = .destructive
        return options
    }

    // MARK:- Swipe Actions
    func updateModel(at indexPath: IndexPath) {
        // overridden in VC
    }
}

2 个答案:

答案 0 :(得分:0)

更有效的解决方案:

将序列从表视图单元格连接到目标控制器,而不是从视图 controller 连接到目标控制器,删除didSelectRowAt并将prepare(for替换为

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToItems" {
        let destination = segue.destination as! ToDoListTVC
        let cell = sender as! SwipeTableViewCell
        if let indexPath = tableView.indexPath(for: cell) {
            destination.selectedCategory = categories?[indexPath.row]
        }
    }
}

答案 1 :(得分:0)

我发现了问题。我不小心将tableView selection更改为no selection。我的错。这不是代码。