tableViewCell中的UIButtons,如何使用Swift从Parse获取已经关注的用户

时间:2015-08-07 04:11:13

标签: ios swift parse-platform uibutton

我已经从2周开始研究它,我喜欢Instagram,用户可以使用Parse后端登录和登录。现在注册后,为下一个名为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 = // What should i use here?? *****************

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

                    }




                })

                }
                }


        })




    }
  }

}

将代码中的问题显示为注释..或者你可以请一些比这更好的方式给我建议..我也尝试在数组中检索数据,还有我遇到的问题“ 纽扣。您可以在此处查看问题.. Not able to reload data properly when retrieving objects from Parse ..请检查此链接..

请帮帮我..谢谢..

编辑:我只想制作具有性别全名和个人资料图片的用户列表,并希望允许用户使用每个单元格上的“关注”按钮关注它们。如果应用程序重新启动,则用户如果当前用户需要,则必须将当前用户标记为“取消关注”以取消关注用户..

检查代码中的注释..错误在那里你看到“***************”它在cellForRowAtIndexPath

1 个答案:

答案 0 :(得分:0)

不要使用PFUser,数组和字典,而是在应用程序端创建一个类User

class User: NSObject {

  var pfUser : PFObject?
  var isFollowing : Bool = false
}

loadData方法中,

func loadData() {

  // **************** Your Codes ********************

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

        var otherUser = User()
        otherUser.pfUser = user

        // **************** Your Codes ********************

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

                       if let objects = objects {

                              if objects.count > 0 {

                                  otherUser.isFollowing = true
                              }
                       }
         })

       self.data.addObject(otherUser)
    }
}

然后使用单个mutableArray data填充tableView