2015-11-01 34 views
0

我试图通过解析在我的应用程序上实现类似于Facebook或Instagram的按钮功能。我尝试使用下面的代码,它的工作原理。当用户点击一个对象上的按钮(或者在我的情况下为消息)时,类似的情况会上升1点。但是,当用户退出应用程序并启动应用程序时,他们可以再次喜欢同一个对象,这意味着他们可以随意多次访问。我是否需要在此代码中编辑某些内容或尝试完全不同的方法?像按钮功能通过解析不工作

@IBAction func likeButton(sender: UIButton) { 
    sender.enabled = false 
    sender.userInteractionEnabled = false 
    sender.alpha = 0.5 

    //get the point in the table view that corresponds to the button that was pressed 
    //in my case these were a bunch of cells each with their own like button 
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView) 
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint) 
    let object = objectAtIndexPath(hitIndex) 

    //this is where I incremented the key for the object 
    object!.incrementKey("count") 
    object!.saveInBackground() 

    self.tableView.reloadData() 
    NSLog("Top Index Path \(hitIndex?.row)") 
} 
+2

也许你可以尝试使用数组或关系来跟踪所有的每个用户都有职位的喜欢,然后创建后单元格时,检查数组或关系是否已经包含该帖子。如果是这样,请防止用户再次喜欢它。 – Acoop

+0

这是我在Parse上做的事吗? – Cooni

回答

0

这里是我会处理这个问题的方式:

首先,我想使禁用按钮到它自己的功能,像这样:

func disableButton(button: UIButton){ 
    button.enabled = false 
    button.userInteractionEnabled = false 
    button.alpha = 0.5 
} 

(当用户按类似的按钮它被禁用像这样:)

@IBAction func likeButton(sender: UIButton) { 
    disableButton(sender) 

    //get the point in the table view that corresponds to the button that was pressed 
    //in my case these were a bunch of cells each with their own like button 
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView) 
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint) 
    let object = objectAtIndexPath(hitIndex) 

    //this is where I incremented the key for the object 
    object!.incrementKey("count") 
    object!.saveInBackground() 

    self.tableView.reloadData() 
    NSLog("Top Index Path \(hitIndex?.row)") 
} 

然后,我会使用户的属性ir喜欢帖子,这是这些帖子的objectIds的一串字符串。 (因为用户可能会喜欢很多帖子,所以你真的应该使用关系,但是数组更容易理解,PFObjects之间关系的文档是here。)然后,当为每个帖子创建单元格时,其中cell是单元格创建,post是目前的职位列表和cell.likeButton是从细胞等等按钮:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell = YourCell() 
    let post = posts[indexPath.row] 
    if (PFUser.currentUser!["likedPosts"] as! [String]).contains(post.objectId){ 
     cell.disableButton(cell.likeButton) 
    } 
    //Setup rest of cell 
+0

不,这只是一个正常的功能。 – Acoop

+0

您是否还在cell.disableButton的单元格中创建了一个插座? – Cooni

+0

如果将disableButton添加到单元格子类中,则点符号将访问它。 – Acoop