如何根据PFRelation更改图像?

时间:2015-11-12 19:09:04

标签: ios swift parse-platform

我有一个带有搜索栏的集合视图,可以在数据库中搜索搜索到的用户。我目前可以获取搜索到的用户。单元格有一个跟随按钮,当单击该按钮时,将创建PFRelation,并且跟随按钮变为跟随按钮(它只是更改按钮的图像)。我的问题是每当用户搜索另一个用户,并且他已经关注用户时,仍然会出现关注按钮,而我希望它有一个跟随按钮作为图像。以下是我的代码。

var relation: PFRelation = (PFUser.currentUser()?.relationForKey("Friendship"))!

func followuser(sender: UIButton){
    let followbtn = sender
    let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.collectionview)
    switch(buttonState){
    case 0:
    if var indexPath :NSIndexPath = self.collectionview.indexPathForItemAtPoint(pointInTable){
        print(pointInTable)
        print(indexPath.item)

        relation.addObject(users[indexPath.item])
         print(relation)

        PFUser.currentUser()?.saveInBackground()
        followbtn.setImage(UIImage(named: "Following"), forState: .Normal)
        }
    buttonState = 1;
    break;
    case 1:
       if var indexPaths :NSIndexPath = self.collectionview.indexPathForItemAtPoint(pointInTable){
        followbtn.setImage(UIImage(named: "Follow"), forState: .Normal)
        relation.removeObject(users[indexPaths.item])
        PFUser.currentUser()?.saveInBackground()
       }
        buttonState = 0;
    default: break
    }

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return users.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionview.dequeueReusableCellWithReuseIdentifier("searchcell", forIndexPath: indexPath) as! searchcustomcell

    cell.follow.addTarget(self, action: "followuser:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.follow.tag = indexPath.item


    if let value = users[indexPath.item]["username"] as? String{
        cell.searchname.text = value
    }

    if let value = users[indexPath.item]["ProPic"] as? PFFile{
        let finalImage = users[indexPath.item]["ProPic"] as? PFFile
        finalImage!.getDataInBackgroundWithBlock(){
            (imageData: NSData?, error: NSError?) -> Void in
            if error == nil{
                if let imageData = imageData{
                    cell.searchimage.image = UIImage(data: imageData)
                    cell.searchimage.layer.cornerRadius = cell.searchimage.frame.size.width / 2;
                    cell.searchimage.clipsToBounds = true
                    }
            }
        }

    }



    return cell
}

1 个答案:

答案 0 :(得分:1)

我建议查询"友谊"关系,并将结果存储为数组 之前开始任何搜索(即viewDidLoad)。

您可以将其存储为用户ID数组,而不是PFUser数组。 我们称之为friends_ids

var friends_ids: [String] = []

let query = relation.query()
query.findObjectsInBackgroundWithBlock{
    (objects: [AnyObject]?, error: NSError?) -> Void in
    if let objects = objects {
        friends_ids = objects.map({ (u) -> String in
            return u.objectId
        })
    }
}

每当您显示一个单元格(cellForItemAtIndexPath)时,您应该检查users[indexPath.item]数组中是否包含friends_ids的ID。

if friends_ids.contains(users[indexPath.item].objectId){
    // hide/change 'follow' button
}

如果它在那里,你可以隐藏"关注"按钮,做任何你喜欢的事。