DidSelectRowAt не вызывается в UITableViewController - PullRequest
0 голосов
/ 14 декабря 2018

Я знаю, что типичным решением этого является присвоение 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 голосов
/ 14 декабря 2018

Я нашел проблему.Я случайно изменил tableView selection на no selection.Виноват.это не код.

0 голосов
/ 14 декабря 2018

Более эффективное решение:

Подключите последовательность из табличного представления ячейка к контроллеру назначения, а не из представления контроллер , удалите 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]
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...