0

I have a tableView that triggers a segue to a detail view when a cell is pressed. The table contains a list of users that the currentUser is friends with. Pressing the cell loads the view for that user (name, profile, etc). It's a very simple app I'm creating to learn how to program.

The problem is that when I press on a cell, it's always loading the user info for the last user in the table (and also happens to be the most recent "friend" that the user made). I have a feeling that the issue is with an if statement I have in the tableView function:

extension MatchesViewController: UITableViewDataSource
{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return numberOfMatches
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("MatchCell", forIndexPath: indexPath)

        if PFUser.currentUser()!.objectId == self.user1.objectId{
            let user = matchesResults[indexPath.row]["user2"] as! PFUser
            cell.textLabel?.text = user["first_name"] as! String
            self.viewUser = user
        }

        if PFUser.currentUser()!.objectId == self.user2.objectId{
            let user = matchesResults[indexPath.row]["user1"] as! PFUser
            cell.textLabel?.text = user["first_name"] as! String
            self.viewUser = user
        }

        return cell
    }

    }

Here's the segue code, but I don't think there is an issue with it (although I could be wrong):

extension MatchesViewController: UITableViewDelegate
{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {   
        self.performSegueWithIdentifier("UserSegue", sender: "viewUser")
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "UserSegue") {
            let destinationVC = segue.destinationViewController as! UserViewController
            destinationVC.user = self.viewUser
        }
    }

    }

Any ideas? If there is an issue with my tableView If statement, how can I fix it?

Thanks!

winston
  • 3,000
  • 11
  • 44
  • 75

1 Answers1

1

You can get the user object by indexPath, than pass it through the sender parameter of the performSegueWithIdentifier method

extension MatchesViewController: UITableViewDelegate
{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {   
       let user:PFUser?
       if PFUser.currentUser()!.objectId == self.user1.objectId{
           user = matchesResults[indexPath.row]["user2"] as! PFUser
       }

        if PFUser.currentUser()!.objectId == self.user2.objectId{
           user = matchesResults[indexPath.row]["user1"] as! PFUser
       }
       self.performSegueWithIdentifier("UserSegue", sender: user)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       // sender is the user object
        if (segue.identifier == "UserSegue") {
            let destinationVC = segue.destinationViewController as! UserViewController
            destinationVC.user = sender  // maybe you need cast sender to UserObject
        }
    }

}
t4nhpt
  • 5,264
  • 4
  • 34
  • 43
  • Thanks for the reply! I get `fatal error: unexpectedly found nil while unwrapping an Optional value` on the line `let user = cell!.tag`. – winston Nov 02 '15 at 03:17
  • Why is `cell` nil? You select a cell, I think you must get the selected cell. – t4nhpt Nov 02 '15 at 03:20
  • I changed the `let cell` back to `let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("MatchCell", forIndexPath: indexPath)` but I kept the other lines of your solution and it seemed to get me a little further. When I load the tableView, XCode for some reason segues to the user View without pressing the cell and shows a nil error on the username label – winston Nov 02 '15 at 03:25
  • See this to get the selected cell: http://stackoverflow.com/questions/26158768/how-to-get-textlabel-of-selected-row-in-swift – t4nhpt Nov 02 '15 at 03:30
  • I think I'm close. When I click on a cell, I get an error `Could not cast value of type '__NSCFNumber' (0x110a60130) to 'PFUser' (0x10f41ccc0).` It errors on the line `destinationVC.user = sender as! PFUser`. For some reason it doesn't like sender. – winston Nov 02 '15 at 03:40
  • THAT WORKED! Thank you so much! I have been stuck on this problem longer than I'd like to admit. – winston Nov 02 '15 at 03:55