首页 > 解决方案 > 没有在 UITableViewController 中调用 DidSelectRowAt

问题描述

我知道对此的典型解决方案是将 的delegate分配tableViewUIViewController. 这里的问题是,这实际上是一个UITableViewController. 话虽如此,这就是问题所在。

我有CategoriesTVC继承自SwipeTVCSwipeTVC的超类是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
    }
}

标签: swiftuitableview

解决方案


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


推荐阅读