User list to follow using parse and swift

笑着哭i 提交于 2020-01-01 19:52:29

问题


I've been working on it from 2 weeks now, I've Instagram like app, where user can signUp and Login using Parse backend. Now after signUp a segue is made for the next screen called "userListTableViewController" here users will be allowed to follow the already existing users. Now i trying to retrieve data in NSMutableArray and show them as follows:

import UIKit

class userListTableViewController: UITableViewController {

var data:NSMutableArray = NSMutableArray()
var isFollowing = [PFObject:Bool]()






func loadData() {

    data.removeAllObjects()
    isFollowing.removeAll(keepCapacity: true)


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

        if error != nil {

        } else {

            if let objects = objects {
                 for object in objects {

                    if let user = object as? PFObject {

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

                            self.data.addObject(user)




                            var followerQuery: PFQuery = PFQuery(className: "Followers")
                                followerQuery.whereKey("follower", equalTo: PFUser.currentUser()!)
                                followerQuery.whereKey("user", equalTo: user)

                            followerQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                                if let objects = objects {

                                        if objects.count > 0 {

                                            self.isFollowing[user] = true


                                } else {

                                            self.isFollowing[user] = false


                                    }

                                }

                                    if self.isFollowing.count == self.data.count {

                                        self.tableView.reloadData()

                                    }


                                })


                            }


                        }

                    }

                }

            }

    })

}

override func viewDidLoad() {
    super.viewDidLoad()

    loadData()

}


/*
var array:NSMutableArray = NSMutableArray(array: self.data.reverseObjectEnumerator().allObjects)
self.data = array as NSMutableArray

self.tableView.reloadData()

*/



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return data.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("users", forIndexPath: indexPath) as! userListTableViewCell

    let userData:PFObject = self.data.objectAtIndex(indexPath.row) as! PFObject


    myCell.fullName.text = userData["objectId"] as! String!
    myCell.genderLabel.text = userData["gender"] as! String!

   // var array:NSMutableArray = NSMutableArray(array: self.data.reverseObjectEnumerator().allObjects)
  //  self.data = array as NSMutableArray



    let followedId = userIds[indexPath.row]

    if isFollowing[followedId] == true { // ERROR!! not able to use followedId here.. how should i give a value so that it will check the particular user's objectId and isFollowing Bool value. 

        myCell.followButtton.setTitle("unfollow", forState: UIControlState.Normal)

    } else {

        myCell.followButtton.setTitle("follow", forState: UIControlState.Normal)


    }

    userData["profilePicture"]?.getDataInBackgroundWithBlock({ (data, error) -> Void in

        if let downloadeImage = UIImage(data: data!) {

            myCell.dp.image = downloadeImage
        }

    myCell.followButtton.tag = indexPath.row
    myCell.followButtton.addTarget(self, action: "followButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)

        })



    return myCell
}

// IBActions..


func followButtonTapped(sender: UIButton){

    let userData:PFObject = self.data.objectAtIndex(sender.tag) as! PFObject

    let followedId = userData["objectId"] as! PFObject

    if isFollowing[followedId] == false {

        isFollowing[followedId] = true

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



        let getObjectByIdQuery = PFUser.query()
        getObjectByIdQuery?.whereKey("objectId", equalTo: userData.objectId!)
        getObjectByIdQuery?.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.saveInBackgroundWithBlock({ (success, error) -> Void in
                    if error != nil {
                        println(error)

                    } else {

                        println("saved")

                    }

                })


            }


        })


    } else {

        isFollowing[followedId] = false
        sender.setTitle("follow", forState: UIControlState.Normal)

        var query:PFQuery = PFQuery(className: "Followers")
        query.whereKey("follower", equalTo: PFUser.currentUser()!)
        query.whereKey("user", equalTo: data[sender.tag])
        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if let objects = objects {

                for object in objects {

                    object.deleteInBackgroundWithBlock({ (success, error) -> Void in

                        if error != nil {
                            println(error)

                        } else {

                            println("deleted")

                        }




                    })

                    }
                    }


            })




}
}

}

shown the problem in the code as comment.. or else canyou please suggest me some other way better than this.. i ve tried to retrieve data in arrays also, there also i'm getting problem for the "follow" buttons. You can see the problem here..Not able to reload data properly when retrieving objects from Parse

Please help me out here.. Thanks..

Edit: I just want to make the user list with there gender full name and profile picture, and want to allow user to follow them with a button "follow" on each cell.. and if the app restarts, the users which have been already followed by the current user must be marked as "unfollow" to unfollow the user if current user wants it..

来源:https://stackoverflow.com/questions/31836201/user-list-to-follow-using-parse-and-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!