试图让我的投票系统工作并在Parse中登录后端

时间:2015-05-25 07:43:28

标签: ios xcode swift vote-up-buttons

我正在尝试创建一个投票系统并将其存储到我的后端,并为每张图片提供它并为每张图片存储。我在后端Parse中创建了一个名为" count"但我似乎无法将投票提出或保存在后面并添加。我正在使用swipeGestureRecognizer来启动一个向上一个向下的投票权,但是我无法在switch语句中获得正确的语法 - =和+ =并且我得到二进制运算符' + =&的错误#39; /' - ='不能应用于类型' [(Int)]'的操作数和' Int'我能做些什么来使投票系统能够在后端保存并提出并显示每个单独的图片投票?

import UIKit
import Parse


class HomePage: UITableViewController {

    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.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil  {

                println("Successfully retrieved \(objects!.count) scores.")
                println(objects!)
                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:
                      count += 1
                    println("Swiped right")
                case UISwipeGestureRecognizerDirection.Left:
                     count -= 1
                    println("Swiped Left")
                default:
                    break
                }
            }
        }

    }

这就是我现在所拥有的但是投票仍然不会被记录到解析中并且post.count + = 1和post.count- = 1正在接收错误消息' PFObject'没有名为' count'的成员我哪里错了?

parse

1 个答案:

答案 0 :(得分:2)

首先,您在屏幕上显示的内容应完全取决于您的Parse模型。我的意思是 - 每次用户投票时你都会增加一个count属性。如果用户在此屏幕中停留一小时会发生什么,到那时还会有10个用户投票?这不会在当前屏幕中更新,用户将看到不是最新的投票。

所以你可以做的是创建一个继承自PFObject的对象。此对象将与Parse绑定,并始终是最新的。

你可以开始阅读the documentationThis也可以为您提供帮助。

所以主要的想法是让你的Parse列作为PFObject子类的属性:

class Post: PFObject, PFSubclassing {
    @NSManaged var count: Int

    class func parseClassName() -> String! {
        return "Post"
    }
}

AppDelegate的{​​{1}}方法中注册您的Parse类:

application(_:didFinishLaunchingWithOptions:)

当您想要更改 count 属性时,您必须先设置它,然后更新屏幕:

Post.registerSubclass()