我正在尝试使用解析在我的应用程序中实现类似的功能。如果用户点击投票按钮。标签也增加了在解析端改变类似的数字。但是,使用我的代码,用户可以多次点击以增加点赞。我想让它检测到用户已点击并禁用类似按钮。为此,我在解析中创建了一个名为“Liked”的类。我创建了一个用户名, imageId 既是一个字符串列,又是一个 likeStatus 作为 Boolean 。但是我不能这样做,如果用户喜欢任何图像,它将使用 userId、ImageId 和 likeStatus 添加新项目。 这是 Collection View 代码
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newview", forIndexPath: indexPath) as! NewCollectionViewCell
let item = self.votes[indexPath.row]
// Display the country name
if let value = item["imageText"] as? String {
cell.postsLabel.text = value
}
// Display "initial" flag image
var initialThumbnail = UIImage(named: "question")
cell.postsImageView.image = initialThumbnail
cell.complition = {
self.likeButton(indexPath)
}
if let votesValue = item["votes"] as? Int
{
cell.votesLabel?.text = "\(votesValue)"
}
// Fetch final flag image - if it exists
if let value = item["imageFile"] as? PFFile {
cell.postsImageView.file = value
cell.postsImageView.loadInBackground({ (image: UIImage?, error: NSError?) -> Void in
if error != nil {
cell.postsImageView.image = image
}
})
}
return cell
}
/*
==========================================================================================
Segue methods
==========================================================================================
*/
func likeButton(indexPath:NSIndexPath)
{
let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as! NewCollectionViewCell
let object = self.votes[indexPath.row]
if let likes = object["votes"] as? Int
{
object["votes"] = likes + 1
object.saveInBackgroundWithBlock{ (success:Bool,error:NSError?) -> Void in
println("Data saved")
}
cell.votesLabel?.text = "\(likes + 1)"
}
else
{
object["votes"] = 1
object.saveInBackgroundWithBlock{ (success:Bool,error:NSError?) -> Void in
println("Data saved")
}
cell.votesLabel?.text = "1"
}
}
这是单元格代码
@IBAction func vote(sender: AnyObject) {
if self.complition != nil
{
self.complition!()
}
}
}
任何提示或我如何能够在代码中执行此操作?谢谢。
最佳答案
我这样做的方法是在 Parse 中使用一个类,我称之为“UserLikeActivity”或类似的东西,在其中,它有一个指向喜欢的用户的列指针,一个指向喜欢的事件的指针被点赞(在我的例子中是一个帖子)、一个类型(表示它是赞成票、反对票、关注等),以及指向创建被赞事件的用户的指针。
现在,当我查询 Parse 来设置我的表时,我不仅查询了包含所有帖子的类,而且还查询了这个类,然后我将其保存并用于确定按钮状态。因此,对于每个单元格,如果该事件已经被点赞,我就会禁用该按钮。希望这会帮助您朝着正确的方向前进,因为您已经问了大约 7 次这个问题。
关于ios - 解析检查用户是否已经点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31749722/