didSelectRowAt не вызывается для моего представления таблицы - PullRequest
0 голосов
/ 25 апреля 2019

Я хочу сделать переход от ячейки UITableView к контроллеру представления. Мой didSelectRowAt не работает.

Вот код:

import UIKit

class ViewController: UIViewController, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    var goals = [String]()

    @IBAction func onAddTapped() {
        let alert = UIAlertController(title: "Add Goal", message: nil, preferredStyle: .alert)
        alert.addTextField { (dessertTF) in
            dessertTF.placeholder = "Enter Goal"
        }
        let action = UIAlertAction(title: "Add", style: .default) { (_) in
            guard let goal = alert.textFields?.first?.text else { return }
            print(goal)
            self.add(goal)
        }
        alert.addAction(action)
        present(alert, animated: true)
    }

    func add(_ goal: String) {
        let index = 0
        goals.insert(goal, at: index)

        let indexPath = IndexPath(row: index, section: 0)
        tableView.insertRows(at: [indexPath], with: .left)
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        performSegue(withIdentifier: "segue", sender: self)
    }

}

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return goals.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let dessert = goals[indexPath.row]
        cell.textLabel?.text = goals
        return cell
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        guard editingStyle == .delete else { return }
        desserts.remove(at: indexPath.row)

        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

1 Ответ

0 голосов
/ 26 апреля 2019

Проблема в том, что вы неправильно создаете свои ячейки.

Внутри вашего cellForRow изменения функции let cell = UITableViewCell()

к этому:

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

Убедитесь, что в файле раскадровки вы установили идентификатор ячейки "Ячейка"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...