gpt4 book ai didi

ios - 如何根据 PFRelation 更改图像?

转载 作者:行者123 更新时间:2023-11-29 01:28:58 25 4
gpt4 key购买 nike

我有一个带有搜索栏的 Collection View ,可以在数据库中搜索搜索到的用户。我目前可以获得搜索到的用户。该单元格有一个关注按钮,当单击该按钮时,将创建 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
}

最佳答案

我建议查询“Friendship”关系,并将结果存储为数组之前你开始任何搜索(即在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] 的 id 是否包含在您的 friends_ids数组。

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

如果它在那里,你可以隐藏“关注”按钮,随心所欲。

关于ios - 如何根据 PFRelation 更改图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33679325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com