How do I get the voting system code to work with Parse?
I am trying to create a voting system so that I can register votes received in a certain way and display them in a cell.
I can't seem to get my vote to work correctly. I am trying to use the operands + = and - = because I could not calculate the number of increments, but I keep getting an error in the message. count + = or - = 1 from:, PFObject does not have a member named count
which I do in my browser:
Here is my code that I have so far:
import UIKit
import Parse
class HomePage: UITableViewController {
let post = PFObject(className: "Post")
var images = [UIImage]()
var titles = [String]()
var imageFile = [PFFile]()
var count = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
println(PFUser.currentUser())
var query = PFQuery(className:"Post")
query.orderByDescending("createdAt")
query.limit = 15
query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) scores.")
println(objects!)
if let objects = objects as? [PFObject] {
for object in objects {
if let title = object["Title"] as? String {
self.titles.append(title)
}
if let imgFile = object["imageFile"] as? PFFile {
self.imageFile.append(imgFile)
}
if let voteCounter = object["count"] as? Int {
self.count.append(voteCounter)
}
self.tableView.reloadData()
}
} else {
// Log details of the failure
println(error)
}
}
}
}
/* println("Successfully retrieved \(objects!.count) scores.")
for object in objects! {
self.titles.append(object["Title"] as! String)
self.imageFile.append(object["imageFile"] as! PFFile)
self.tableView.reloadData()
}*/
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 500
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! cell
myCell.rank.text = "21"
myCell.votes.text = "\(count)"
myCell.postDescription.text = titles[indexPath.row]
imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
myCell.postedImage.image = downloadedImage
}
}
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
myCell.postedImage.userInteractionEnabled = true;
myCell.postedImage.addGestureRecognizer(swipeRight)
var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Left
myCell.postedImage.userInteractionEnabled = true;
myCell.postedImage.addGestureRecognizer(swipeLeft)
return myCell
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
post.count += 1
println("Swiped right")
case UISwipeGestureRecognizerDirection.Left:
post.count -= 1
println("Swiped Left")
default:
break
}
}
}
}
How do I get the counter to work in this code? Why do I keep getting this error and how can I log the counter for each specific image?
any type of voting system code would be appreciated, I can change it, I just want to keep the swipe gestures as the way you move up and down.
source to share
There are several rows in the table (at least four) that have no " count
" parameters . Remove them or change the code:
var potentialVoteCounter : Int? = object["count"]
if potentialVoteCounter == nil {
// create "count" in this Parse object
let zero:NSNumber = 0
object["count"] = zero
}
if let voteCounter = object["count"] as? Int {
self.count.append(voteCounter)
}
or
if let voteCounter = object["count"] as? Int {
// do nothing here...
} else {
// create "count" in this object
let zero:NSNumber = 0
object["count"] = zero;
}
if let voteCounter = object["count"] as? Int {
self.count.append(voteCounter)
}
to keep the updated Parse object at the end (so the table will reflect the changes you made)
source to share