Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

ios - Not able to reload data properly when retrieving objects from Parse

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...