简体   繁体   English

在Swift中使用Parse投票按钮

[英]Voting buttons with Parse in Swift

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. 我有一个PFQueryTableviewController ,它使用自定义单元格。 This table shows films which come from my Parse database. 下表显示了来自我的Parse数据库的电影。 In each cell, there a 3 UIButtons, of which I want the user to use to vote for the film (happyVote, OkVote, sadVote). 在每个单元格中,有3个UIButton,我希望用户用来为电影投票(happyVote,OkVote,sadVote)。 Over the top of each button is a UILabel that simply displays a count. 每个按钮上方都有一个UILabel,它仅显示一个计数。

How I want it to work 我希望它如何工作

  1. When a user presses one of the buttons, the vote increases by 1. 当用户按下按钮之一时,投票将增加1。
  2. When a user presses the same button again, the vote decreases by 1. Or, 当用户再次按下相同的按钮时,投票将减少1。或者,
  3. If the user had pressed a different button, the vote decreases on the first button and increases on the button just pressed. 如果用户按下了其他按钮,则第一个按钮的投票减少,而刚按下的按钮的投票增加。
  4. The user can only ever vote on one of the buttons. 用户只能对按钮之一进行投票。
  5. The vote is shown by the UILabel showing the count, and by the button image changing. 投票通过显示计数的UILabel以及按钮图像的更改来显示。

See below for a visual: 参见下面的视觉效果:

在此处输入图片说明

This is what I've added in Parse: 这是我在Parse中添加的内容:

在此处输入图片说明

So far, I've added the code to increase the vote count in Parse, in my TableViewController.swift : 到目前为止,我已经在TableViewController.swift添加了代码来增加Parse中的投票计数:

@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()

}

This kind of works, except, the user can keep increasing the count, and they can vote on all 3 buttons. 除了用户可以不断增加计数,而且可以对所有3个按钮进行投票之外,这种工作方式也是如此。 And it doesn't change the Button Image when pressed. 按下时它不会更改按钮图像。

In my cellForRowAtIndexPath method, I've put: 在我的cellForRowAtIndexPath方法中,我输入了:

 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 谢谢

In your upVoteButton() method, you can add the other two buttons and set them button.enabled = false inside an if statement like: 在您的upVoteButton()方法中,您可以添加其他两个按钮并将它们设置为if语句中的button.enabled = false例如:

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

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. 并在其他if语句中使用middleVoteButton进行相同操作,或者使用相同的按钮在其他按钮方法中执行浮动操作。 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 . 对于图像更改,您可以按照此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM