user10121087
user10121087

Reputation:

Cells in table view not responding

I am working on a todo list app and whenever I run it on the simulator and try to print the items in my array, the other cells item get printed.

Here's my code:

import UIKit

class TodoListViewController: UITableViewController {
    let itemArray = ["math","english"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    // DATASOURCE METHODS

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  itemArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
        cell.textLabel?.text = itemArray[indexPath.row]
        return cell
    }

    // DELEGATE METHODS

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print(itemArray[indexPath.row])
    }


}

Upvotes: 1

Views: 60

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need didSelectRowAt not didDeselectRowAt , the latter is triggered when you select a row so you get the print from the previous selected row

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(itemArray[indexPath.row])
}

Upvotes: 2

Related Questions