从解析查询后设置跟随按钮标题 [英] Set follow button title after query from parse

查看:89
本文介绍了从解析查询后设置跟随按钮标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的_User类看起来像这样:

在followedPeople列中,我保存了当前用户紧随其后的用户的objectIds.我的整个查询看起来像这样:

var data:NSMutableArray = NSMutableArray()
var followedPeople = [NSArray]()

func loadData() {

    self.followedPeople.removeAll(keepCapacity: true)
    self.data.removeAllObjects()

    var usersQuery:PFQuery = PFUser.query()!

        usersQuery.orderByAscending("createdAt")
        usersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if error == nil {

            if let objects = objects as? [PFObject] {
                for object in objects {
                    if let user = object as? PFUser {

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

                            if object["followedPeople"] == nil {
                                println("nil value")                                    
                            } else {
                                self.followedPeople.append(object.objectForKey("followedPeople")! as! NSArray)
                                println(self.followedPeople)
                            }
                        } else {
                            self.data.addObject(object)
                            println(self.data)
                        }
                    }

                    self.tableView.reloadData()
                }
            }
        } else {
            // Log details of the failure.
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
}

我的问题是,我要保存所有用户的objectIds,然后是当前用户,我想在tableView中使用这些objectIds,以便如果用户位于NSArray中,则这些按钮的跟随按钮标题用户应该是取消关注",基本上我是想向用户显示他是否已经在关注某人..我的tableView代码是:

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


    // Usernames and gender..

    myCell.fullName.text = userData.objectForKey("fullName") as! String!
    myCell.genderLabel.text = userData.objectForKey("gender") as! String!
    myCell.userTypeLabel.text = userData.objectForKey("userType") as! String!

    // Profile pictures..

    let profilePics = userData.objectForKey("profilePicture") as! PFFile
    profilePics.getDataInBackgroundWithBlock { (data, error) -> Void in

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

            myCell.dp.image = downloadedImage
        }

    }

    // here i want to set the button title..like:
    // if current user is following the user on the cell {
    //      myCell.followButton.setTitle("following", .normal)
    //   } else {
    //      myCell.followButton.setTitle("follow", .normal)
    // }

    myCell.createButton(self)

    return myCell

解决方案

我认为您关心两个数组:(1)所有用户的数组,以及(2)当前用户之后的用户数组.

查找发现所有用户.您发现的逻辑应该改变.只需将所有找到的用户放入data数组即可.

在索引路径行的单元格中,您有...

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

要确定当前用户是否正在跟踪该用户,只需检查该用户的ID是否为跟随的用户ID数组即可.伪伪(我真的不说)...

var myFollowedUserIds = PFUser.currentUser["followedUsers"]
var userIsFollowed /* this is a bool */ myFollowedUserIds.containsObject(userData.objectId)

现在,用userIsFollowed配置单元格,说类似...

var buttonTitle = userIsFollowed?  "Follow" : "Unfollow"

您可能会考虑通过保留指向实际关注用户的指针数组(而不是该followedUsers列中的字符串ID)来改进设计.好处之一是,当您获得当前用户时,可以急切地获取它们.

So my _User class looks like this :

In the followedPeople column i've saved the objectIds of the user which is followed by the current user. My whole query looks like this:

var data:NSMutableArray = NSMutableArray()
var followedPeople = [NSArray]()

func loadData() {

    self.followedPeople.removeAll(keepCapacity: true)
    self.data.removeAllObjects()

    var usersQuery:PFQuery = PFUser.query()!

        usersQuery.orderByAscending("createdAt")
        usersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if error == nil {

            if let objects = objects as? [PFObject] {
                for object in objects {
                    if let user = object as? PFUser {

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

                            if object["followedPeople"] == nil {
                                println("nil value")                                    
                            } else {
                                self.followedPeople.append(object.objectForKey("followedPeople")! as! NSArray)
                                println(self.followedPeople)
                            }
                        } else {
                            self.data.addObject(object)
                            println(self.data)
                        }
                    }

                    self.tableView.reloadData()
                }
            }
        } else {
            // Log details of the failure.
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
}

My problem is, I'm saving the objectIds of all the users followed by the current user and i want to use those objectIds in the tableView so that if the user is in the NSArray then the follow button's title of those users should be "Unfollow", basically i'm trying to show the user if he's already following someone.. My tableView code is:

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


    // Usernames and gender..

    myCell.fullName.text = userData.objectForKey("fullName") as! String!
    myCell.genderLabel.text = userData.objectForKey("gender") as! String!
    myCell.userTypeLabel.text = userData.objectForKey("userType") as! String!

    // Profile pictures..

    let profilePics = userData.objectForKey("profilePicture") as! PFFile
    profilePics.getDataInBackgroundWithBlock { (data, error) -> Void in

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

            myCell.dp.image = downloadedImage
        }

    }

    // here i want to set the button title..like:
    // if current user is following the user on the cell {
    //      myCell.followButton.setTitle("following", .normal)
    //   } else {
    //      myCell.followButton.setTitle("follow", .normal)
    // }

    myCell.createButton(self)

    return myCell

解决方案

I think you care about two arrays: (1) the array of all users, and (2) the array of users followed by the current user.

The find finds all users. You logic after that find should change. Just put all of the found users into the data array.

In cell for row at index path, you have...

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

To determine whether that user is being followed by the current user, just check to see if that user's id is the followed users id array. In pseudo-swift (I don't really speak it)...

var myFollowedUserIds = PFUser.currentUser["followedUsers"]
var userIsFollowed /* this is a bool */ myFollowedUserIds.containsObject(userData.objectId)

Now, configure the cell with userIsFollowed, saying things like...

var buttonTitle = userIsFollowed?  "Follow" : "Unfollow"

You might consider improving your design by keeping an array of pointers to actual followed users, rather than string ids in that followedUsers column. One benefit is that they can be fetched eagerly when you get the current user.

这篇关于从解析查询后设置跟随按钮标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆