2015-05-25 17 views
0

我想創建一個投票系統,並將其存儲到我的後端,並讓它爲每個單獨的圖片出現並存儲爲每張圖片。我在我的後端Parse中創建了一個名爲「count」的列,但我似乎無法得到投票或保存在後面的投票並添加。我正在使用一個swipeGestureRecognizer來啓動一個左上角的投票權,但我無法在 - =和+ =的switch語句中獲得正確的語法,並且我得到了二元運算符錯誤'+ ='/' - ='不能應用於'[(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

回答

2

首先,你在屏幕上顯示的內容應該在你的解析模型完全依賴。我的意思是 - 每次用戶投票時你都會增加一個count屬性。如果用戶在此屏幕上停留一個小時,那麼會有什麼結果呢?到那時還有10個用戶也會投票?這不會在當前屏幕上更新,用戶將看到不是最新的投票。

所以你可以做的是創建一個從PFObject繼承的對象。這個對象將被綁定到Parse並且將始終保持最新狀態。

您可以從the documentation開始。 This也可以幫助你。

所以主要的想法是讓你解析列作爲PFObject子類的屬性:

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

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

在你AppDelegateapplication(_:didFinishLaunchingWithOptions:)方法註冊解析類是這樣的:

Post.registerSubclass() 

當你想改變計數屬性,你將不得不設置它,然後更新屏幕:

let post = PFObject(className: "Post") 
//increment like this 
post.count += 1 
//decrement like this 
post.count -= 1 

//refresh screen 
//call reloadData() 
+0

我確實把它保存在我的postImageView中作爲一個PFObject,我相信所以在這個tableview中一切都不是PFObject的子類? – DanielWolfgangTrebek

+0

是否有任何快捷的文檔即時通訊客戶不熟悉c – DanielWolfgangTrebek

+0

是的,在這裏你可以切換到Swift版本:https://parse.com/docs/ios/guide –