0

I am retrieving data from "_User" class this way:

my declarations ..

 var userIds = [String]()
 var userNames = [String]()
 var profilePics = [PFFile]()
 var gender = [String]()


var userQuery = PFUser.query()
        userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if let objects = objects {

                self.userIds.removeAll(keepCapacity: true)
                self.userNames.removeAll(keepCapacity: true)
                self.profilePics.removeAll(keepCapacity: true)

                for object in objects {

                    if let user = object as? PFUser {
                        if user.objectId != PFUser.currentUser()?.objectId {

                            self.userIds.append(object["objectId"] as! userListTableViewCell)  // getting an error here..  "unexpectedly found nil while unwrapping an Optional value"
                            self.userNames.append(object["fullName"] as! String!)
                            self.profilePics.append(object["profilePicture"] as! PFFile!)
                            self.gender.append(object["gender"] as! String!)

                        }


                    }
                    self.tableView.reloadData()
                }


            }




        })

screen shots of my app[![][1]]1

here when i click on follow button for user "Rfdfbd" then automatically the "unfollow" title appears on user "Ihbj....." also :/ how can i fix this??

Screen Shots of my app..

my IBAction followButton code is here:

@IBAction func followButtonTapped(sender: UIButton) {

    println(sender.tag)

    sender.setTitle("unfollow", forState: UIControlState.Normal)

    let getOjbectByIdQuery = PFUser.query()
    getOjbectByIdQuery!.whereKey("objectId", equalTo: userIds[sender.tag])
    getOjbectByIdQuery!.getFirstObjectInBackgroundWithBlock { (foundObject: PFObject?, error: NSError?) -> Void in

        if let object = foundObject {

            var followers:PFObject = PFObject(className: "Followers")
            followers["user"] = object
            followers["follower"] = PFUser.currentUser()
            followers.saveEventually()

        }
    }
}

I am using sender.tag for the follow button here..

2
  • This seems you have same button reference in different cells.That's why you update the button contents it effects all the places of button reference. Commented Jul 31, 2015 at 5:34
  • can you please elucidate your answer more?? Commented Jul 31, 2015 at 16:42

1 Answer 1

1

I've had this problem before and fixed it by embedding a button in each cell. Inside your UITableView you should try embedding each cell with a UIButton.

First make a custom UITableViewCell in a separate file. Then drag and make an IBOutlet for your UIButton inside your custom cell.

class MyCustomCell: UITableViewCell{
    @IBOutlet weak var followButton: UIButton!
    var isFollowing:Bool = false
    //Declare other cell attributes here like picture, name, gender
    // ......
}

When you query and gather the data for your cells, you can store them in an array in your UITableViewController. For example, var myCellArray = [MyCustomCell](). Then your UITableViewController will look something like this:

var myCellArray = [userListTableViewCell]()

override func viewDidLoad(){
    super.viewDidLoad()

    var userQuery = PFUser.query()
    userQuery.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in

        if let usersArray = objects as! [PFUser] {

            self.myCellArray.removeAll(keepCapacity: false)

            for user in usersArray {

                if let user = object as? PFUser {
                    if user.objectId != PFUser.currentUser()?.objectId {
                        var myCell = userListTableViewCell()
                        myCell.userID = user.objectId
                        myCell.username = user["fullName"] as! String
                        myCell.gender = user["gender"] as! String

                        var userPicture = user["profilePicure"] as? PFFile
                        var image = UIImage(data:userPicture!.getData()!)
                        myCell.displayPicture.image = image

                        myCellArray.append(myCell)
                        self.tableView.reloadData()

                    }
                }
            }
        }
    })
}


override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    myCellArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier") as! userListTableViewCell

    //Edit the storyboard labels for each cell:
    cell.username.text = myCellArray[indexPath.row].username
    // etc....

    //Embed a button with each cell
    cell.followButton.layer.setValue(indexPath.row, forKey: "index")
    cell.followButton.addTarget(self, action: "followButtonTapped:", for ControlEvents: UIControlEvents.TouchUpInside)

    if (myCellArray[indexPath.row].isFollowing == false){
        cell.followButton.setTitle("Follow", forState: .Normal)
    }else{
        cell.followButton.setTitle("Unfollow", forState: .Normal)
    }
    return cell
}

func followButtonTapped(sender: UIButton){
    let cellIndex : Int = (sender.layer.valueForKey("index")) as! Int
    //You now have the index of the cell whose play button was pressed so you can do something like
    if (myCellArray[cellIndex].isFollowing == false){
        myCellArray[cellIndex] = true
    }else{
        myCellArray[cellIndex] = false
    }
    self.tableView.reloadData()
}
Sign up to request clarification or add additional context in comments.

31 Comments

can you elucidate "var myCellArray = [MyCustomCell]()" more??
You would create this inside your UITableViewController class before your functions. On viewDidLoad you should do your Parse query and loop through each PFObject in your Parse class. Inside the loop you can create a MyCustomCell and give it the properties from your Parse query. When you're done with your query, call self.tableView.reloadData() to load your cells.
i've made var userIds = [userListTableViewCell]() and i am getting error here.. self.userIds.append(object.objectId as String!) in my query and rest of the PFObjects by the other arrays i've explained.. like usernames, profilePics, gender.. :/ What am i doing wrong.??
Try using query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in }) function when you do your query. That way you are getting an array of your objects to loop through
Your edited query does not match the declaration I just gave you. You're not getting objects array from your query.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.