使用parse和swift跟随的用户列表

时间:2015-08-05 15:04:53

标签: ios swift parse-platform uibutton

我已经在2周内开始研究它了,我已经像Instagram一样使用了应用程序,用户可以使用Parse后端登录和登录。现在,在signUp之后,为下一个名为" userListTableViewController"的屏幕制作一个segue。这里将允许用户关注已经存在的用户。现在我尝试在NSMutableArray中检索数据并按如下方式显示:

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

                        }




                    })

                    }
                    }


            })




}
}

}

将代码中的问题显示为注释..或者你可以建议我一些比这更好的方法..我也尝试检索数组中的数据,还有我为#&#问题34;按照"纽扣。你可以在这里看到问题.. Not able to reload data properly when retrieving objects from Parse

请帮帮我..谢谢..

编辑:我只想制作包含性别全名和个人资料图片的用户列表,并希望允许用户使用按钮跟随他们"关注"在每个单元格上...如果应用程序重新启动,则必须将当前用户已经关注的用户标记为"取消关注"如果当前用户想要它,请取消关注用户..

0 个答案:

没有答案