简体   繁体   中英

DidSelectRowAtIndexPath not working in Swift 2.0

I have a table view with Static cells (shown below). 在此处输入图片说明

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?

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)
    }
}

Check the section AND row number

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM