ios - 如何让我的投票系统代码与 Parse 一起使用?

标签 ios swift parse-platform

我一直在尝试创建一个投票系统,这样我就可以记录某个图像获得的投票并将它们显示在一个单元格中。

我似乎无法让我的投票正常工作我目前正在尝试使用 += 和 -= 操作数,因为我无法计算出增量计数,但我在发布时不断收到错误消息。 count += or -= 1 of : PFObject does not have a member named count 我在解析后端做的:

Parse

这是我目前的代码:

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

    }

如何在这段代码中进行计数?为什么我不断收到该错误以及如何记录每个特定图像的计数?

任何类型的投票系统代码都将不胜感激我可以更改它我只想保持滑动手势作为你赞成票和反对票的方式。

最佳答案

您的表格有许多行(至少四行)没有任何“count”参数。删除它们,或更改您的代码以执行此操作:

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)
}

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)
}

确保在最后保存更新的 Parse 对象(因此表格将反射(reflect)您所做的更改)

关于ios - 如何让我的投票系统代码与 Parse 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30476654/

相关文章:

ios - 解析 Facebook 登录检查首次登录

javascript - Angular 服务缓存一个值

ios - Sprite Kit 高效的随机节点生成

ios - NSURLConnection/CFURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9843)

swift - 无法单击对象并将其拖动到 ViewController

ios - QuickBlox聊天Swift集成

ios - 从委托(delegate)中检查 UITextField.text?

ios - 如何在导航栏中添加按钮和文本?

iOS - Xcode 中的 JPG "open as"十六进制

Ios- Parse.com 删除一个对象