Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何让我的投票系统代码与Parse一起工作?_Ios_Swift_Parse Platform - Fatal编程技术网

Ios 如何让我的投票系统代码与Parse一起工作?

Ios 如何让我的投票系统代码与Parse一起工作?,ios,swift,parse-platform,Ios,Swift,Parse Platform,我一直在尝试创建一个投票系统,这样我就可以记录某个图像获得的投票并将其显示在单元格中 我似乎无法使我的投票正常工作。我当前正在尝试使用+=和-=操作数,因为我无法计算增量计数,但我一直在post上收到一条错误消息。count+=或-=1:PFObject没有名为count的成员,我在解析后端执行此操作: 以下是我目前掌握的代码: import UIKit import Parse class HomePage: UITableViewController { let post =

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

我似乎无法使我的投票正常工作。我当前正在尝试使用+=和-=操作数,因为我无法计算增量计数,但我一直在post上收到一条错误消息。count+=或-=1:
PFObject没有名为count的成员,我在解析后端执行此操作:

以下是我目前掌握的代码:

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

    }
如何使计数在此代码中起作用?为什么我不断地得到那个错误,我如何记录每个特定图像的计数


任何类型的投票系统代码都将受到欢迎,我可以将其更改为我只想保持向上投票和向下投票时的滑动姿势。

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

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


确保在末尾保存更新的解析对象(以便表反映您所做的更改)

旁注:xcode标记用于IDE问题。让它单独存在@Michael Dautermann我很新我应该在哪里添加代码?在viewDidLoad中?请尝试使用当前代码的位置,并让我知道发生了什么。PotentialVoteCounter未处理错误对象(我将其作为post放置,这是我的PFObject)无法转换为Int。第二个有效,但我仍然在我的switch语句中得到错误,post.count表示post没有名为countYeah的成员,您的解析列需要NSNumber对象,因此我们不能简单地设置
“count”
改为Int。我对代码做了一点修改。我仍然在switch语句中得到post.count的错误,即post没有名为count的成员。我可能没有在else语句中正确创建“count”
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)
}