Iwan Pieterse
Iwan Pieterse

Reputation: 573

iOS: didSelectRowAt Index Path only works on second click

I am struggling with an issue on my xcode project. I'm displaying custom cells in a table view and using didSelectRowAt indexPath: to show a detail view. It works the way I want to, but weirdly only on the second click.

I am fairly new to programming and would appreciate your help with this. Thank you so much!

I checked that I'm not using didDeselectRow at by accident. I also went through stackoverflow to try and find a solution, and that was the closest I could find to my issue. But I am using didSelectRow at.

// Here is my code for when the user taps a cell:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Pop Up Variables

    var id:String = tasks[indexPath.row].task_id!
    var title:String = tasks[indexPath.row].task_title!
    var type:String = tasks[indexPath.row].task!
    var desc:String = tasks[indexPath.row].task_desc!
    var action:String = "Dismiss"


    present(detailVC!, animated: true, completion: {
        self.detailVC!.setPopup(withTaskId: id, withTitle: title, withType: type, withDesc: desc, withAction: action)
         })



}

// Just a note here,I am setting up the switch to the other view controller at the top of the document.

// Display the popup var detailVC:TaskDetailViewController?

override func viewDidLoad() {
    super.viewDidLoad()

    // Set up the task detail view controller
    detailVC = storyboard?.instantiateViewController(withIdentifier: "TaskDetailVC") as! TaskDetailViewController?
    detailVC?.delegate = self
    detailVC?.modalPresentationStyle = .overCurrentContext

    // Conform to the table view protocols
    tableView.dataSource = self
    tableView.delegate = self

    // Set Self as delegate for model and call getTasks

    // Get the Tasks from the Task Model
    model.delegate = self
    model.getTasks()


}

The detail view only appears after I tap a row the second time. Not the first time?

Upvotes: 1

Views: 1281

Answers (2)

Mahdi Moqadasi
Mahdi Moqadasi

Reputation: 2479

Just change Selection of your UITableView in InterfaceBuilder from Multiple Selection to Single Selection and it will works as you wish.

Then you should handle selected state of each row on your own (using a field in model or set color for background or etc)

enter image description here

Upvotes: 1

koropok
koropok

Reputation: 1413

I had this issue a few years back, didn't know that it still persist today.

I solved it by encapsulating all logic within didSelectRowAt in the main thread using GCD. You can check these out:

https://stackoverflow.com/a/27907119/6642629

https://stackoverflow.com/a/26183438/6642629

You can try the following:

Swift 4

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    DispatchQueue.main.async {

        var id:String = tasks[indexPath.row].task_id!
        var title:String = tasks[indexPath.row].task_title!
        var type:String = tasks[indexPath.row].task!
        var desc:String = tasks[indexPath.row].task_desc!
        var action:String = "Dismiss"

        present(detailVC!, animated: true, completion: {
            self.detailVC!.setPopup(withTaskId: id, withTitle: title, withType: type, withDesc: desc, withAction: action)
        })
    }
}

Upvotes: 1

Related Questions