在Swift中使用Parse投票按钮 [英] Voting buttons with Parse in Swift

查看:115
本文介绍了在Swift中使用Parse投票按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要制作的影片的简要概述.我正在制作一个应用来显示我的电影评论.我有一个PFQueryTableviewController,我使用自定义单元格.下表显示了来自我的Parse数据库的电影.在每个单元格中,有3个UIButton,我希望用户用来为电影投票(happyVote,OkVote,sadVote).每个按钮上方都有一个UILabel,它仅显示一个计数.

A quick overview of what I'm trying to make. I'm making an app to show my film reviews. I have a PFQueryTableviewController of which I use a custom cell. This table shows films which come from my Parse database. In each cell, there a 3 UIButtons, of which I want the user to use to vote for the film (happyVote, OkVote, sadVote). Over the top of each button is a UILabel that simply displays a count.

我希望它如何工作

  1. 当用户按下其中一个按钮时,投票将增加1.
  2. 当用户再次按下相同的按钮时,投票将减少1.或者,
  3. 如果用户按下了其他按钮,则第一个按钮的投票减少,而刚按下的按钮的投票增加.
  4. 用户只能对其中一个按钮进行投票.
  5. 投票通过显示标签的UILabel和按钮图像的更改来显示.

有关视觉效果,请参见以下内容:

这是我在解析"中添加的内容:

This is what I've added in Parse:

到目前为止,我已经在我的TableViewController.swift中添加了代码以增加Parse中的投票计数:

So far, I've added the code to increase the vote count in Parse, in my TableViewController.swift:

@IBAction func upVoteButton(sender: AnyObject) {

    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)

    let object = objectAtIndexPath(hitIndex)
    object!.incrementKey("UpVoteCount")
    object!.saveInBackground()
    self.tableView.reloadData()

}

这类作品,除了用户可以不断增加计数,而且可以对所有3个按钮进行投票.并且在按下时它不会更改按钮图像.

This kind of works, except, the user can keep increasing the count, and they can vote on all 3 buttons. And it doesn't change the Button Image when pressed.

在我的cellForRowAtIndexPath方法中,我输入:

In my cellForRowAtIndexPath method, I've put:

 let downVote = object!.valueForKey("DownVoteCount") as! Int
    cell.downVoteLabel.text = "\(downVote)"
    let middleVote = object!.valueForKey("MiddleVoteCount") as! Int
    cell.middleVoteLabel.text = "\(middleVote)"
    let upVote = object!.valueForKey("UpVoteCount") as! Int
    cell.upVoteLabel.text = "\(upVote)"

我已经搜索了一段时间,以寻找一些如何解决其余问题的示例,但找不到任何东西,我真的很努力地想出下一步的解决方法.

I have searched for a while for some examples of how to figure the rest out, but can't find anything, and I'm really struggling to figure the next steps out.

任何帮助将不胜感激,如果您需要/想要查看我的代码,请告诉我.谢谢

Any help will be greatly appreciated, and please let me know if you need/want to see anymore of my code. Thanks

推荐答案

在您的upVoteButton()方法中,您可以添加另外两个按钮并将它们设置在if语句中的button.enabled = false内,例如:

In your upVoteButton() method, you can add the other two buttons and set them button.enabled = false inside an if statement like:

if downVoteButton.enabled == true {
    downVoteButton.enabled = false
} else if downVoteButton.enabled == false {
    downVoteButton.enabled = True
}

在其他if方法中使用适当的按钮,并在if语句中使用middleVoteButton进行相同操作,或者使用相同的按钮进行操作.这有助于在按下按钮时禁用按钮,然后在再次按下按钮时将其启用.

And do the same with the middleVoteButton in antoher if statement or the same, whatever floats your boat, also within the other button methods with the appropriate buttons. This helps disables the buttons when pressed and then enables it when it's pressed again.

对于图像更改,您可以遵循.

And for the image changing you can follow this.

这篇关于在Swift中使用Parse投票按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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