Dawy
Dawy

Reputation: 946

UITableView row action is not called

I created UITableView with 1 section and 3 rows in it. Text in each row is hardcoded and I need to call specific actions by tapping each row. Why UITableView delegae method func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) is not called in following code?

@IBOutlet weak var tbvSetting: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tbvSetting.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
    switch indexPath.row {
    case 0:
        cell.textLabel?.text = "Countries"
    case 1:
        cell.textLabel?.text = "Managers"
    case 2:
        cell.textLabel?.text = "Customers"
    default:
        break
    }
    return cell
}

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

Thanks for hints.

Upvotes: 0

Views: 475

Answers (1)

ystack
ystack

Reputation: 1805

Setup up self as the delegate of the tableView.
You can do it in Storyboard(which seems like you are using) or in code as:

override func viewDidLoad() {
    super.viewDidLoad()

    tbvSetting.delegate = self
    tbvSetting.reloadData()
}


Since, this has not been setup yet, even though tableView cells are getting selected, the OS is unaware of who is the handler of the selection gesture.

Upvotes: 1

Related Questions