Griffin Morandini
Griffin Morandini

Reputation: 53

DidSelectRowAtIndexPath not working in Swift 2.0

I have a table view with Static cells (shown below). enter image description here

I want my application to log the current user out when they select log out, obviously. For the number of sections I have the following code.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var rows: Int = 0

        let numberOfRowsAtSection: [Int] = [2, 2, 1]

        if section < numberOfRowsAtSection.count {
            rows = numberOfRowsAtSection[section]
        }

        return rows
    }

I would normally do didSelectRowAtIndexPath to logout this user so I tried the following code.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if (indexPath.row) == 4 {
            PFUser.logOut()
            dismissViewControllerAnimated(true, completion: nil)
        }

    }

It is not working because the logout is actually at index 0 so when I click either edit profile or about it logs me out because there are 3 index 0's so to speak. I am not sure exactly how I can combat this issue, I tried the code above with no success - any ideas?

Upvotes: 0

Views: 293

Answers (2)

Nishant
Nishant

Reputation: 12617

Re-write your didSelectRowAtIndexPath as shown:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if ((indexPath.section == 2) && (indexPath.row == 0)) {
        PFUser.logOut()
        dismissViewControllerAnimated(true, completion: nil)
    }
}

Upvotes: 3

nielsbot
nielsbot

Reputation: 16022

Check the section AND row number

if indexPath.section == 2 && indexPath.row == 0 {
    /* do log out */
}

Upvotes: 1

Related Questions