2015-06-02 15 views
0

即時通訊使用手勢識別器增加我後端的投票,然後使用它來更新頁面和每個單元格。我正在嘗試將圖像與objectId鏈接起來,以便更新每張圖像投票。問題不斷出現我的updateVote函數,因爲我似乎無法檢索與後端圖像關聯的objectId。我的繼承人的更新票和手勢識別代碼:不能更新沒有特定objectId錯誤使用Swift和解析

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: 
      updateVote(true, objectId: String()) 
      println("Swiped right") 
      case UISwipeGestureRecognizerDirection.Left: 
      updateVote(false, objectId: String()) 
      println("Swiped Left") 
      default: 
       break 
      } 
     } 
    } 

func updateVote(increment: Bool, objectId : String) { 
    // Create a pointer to an object of class Posts with id 'objectId' 
    var object = PFObject(withoutDataWithClassName: "Post", objectId: objectId) 

    // Increment the current value of the quantity key by 1 
    if increment == true { 
     object.incrementKey("count", byAmount: 1) 
    } else { 
     object.incrementKey("count", byAmount: -1) 
    } 

    // Save 
    object.saveInBackgroundWithBlock(nil) 
} 

我怎樣才能找回OBJECTID因爲每當我運行此我不斷收到錯誤沒有具體的對象ID不能更新?我得到了println(「向左或向右滑動」),但我無法獲得投票,我怎麼能做到這一點?

 import UIKit 
     import Parse 


     class HomePage: UITableViewController { 

      var images = [UIImage]() 
      var titles = [String]() 
      var imageFile = [PFFile]() 
      var votingObjects: [PFObject] = [] 
      var objectIds = [""] 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      println(PFUser.currentUser()) 

      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!) 
        for objectRow in objects! { 
         let object = objectRow as! PFObject 


         if let objectIds = object["objectId"] as? String { 
          self.objectIds.append(objectIds) 

         } 





         //objectIds.append(object.objectId as? String) 
         // Adding them to the array 

         if let title = object["Title"] as? String { 
          self.titles.append(title) 
         } 
         if let imgFile = object["imageFile"] as? PFFile { 
          self.imageFile.append(imgFile) 
         } 
         self.votingObjects.append(object) 
        } 
        dispatch_async(dispatch_get_main_queue(), { 
         self.tableView.reloadData() // Updating the tableView on the main thread - important. Do some research on Grand Central Dispatch :) 

        }) 
       } else { 
        println(error) 
        // Error 
       } 
      } 
    } 
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 = votingObjects[indexPath.row]["Post"] as? String 
     myCell.postDescription.text = titles[indexPath.row] 

     imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in 

      if let downloadedImage = UIImage(data: data!) { 

       myCell.postedImage.image = downloadedImage 

      } 
     } 

這是我在該頁面上的其餘代碼如何從數組中檢索objectId?

import UIKit 
import Parse 
import Foundation 

class cell: UITableViewCell { 

    @IBOutlet weak var rank: UILabel! 
    @IBOutlet weak var votes: UILabel! 
    @IBOutlet weak var postedImage: UIImageView! 
    @IBOutlet weak var creditLink: UIButton! 
    @IBOutlet weak var addButton: UIButton! 
    @IBOutlet weak var postDescription: UITextView! 

} 

我的手機類

回答

1

你傳遞一個空字符串作爲的objectID objectId: String()「這樣你就不會選擇任何行進行更新。

你的功能應該通過該行的objectID如此解析可以更新

enter image description here

+0

我應該怎麼把一個空字符串,而不是如果我不知道這照片是刷卡?我不知道確切的objectID,但我知道它會有一個。 – DanielWolfgangTrebek

+0

如果你不知道你會遇到什麼樣的對象,怎麼會解析知道?我無法真正告訴你如何去做,因爲我不明白你是如何檢索你的數據來從你的表中解析出來的。但我可以說的是,在那一刻,你應該保持objectID在這裏傳遞它的值 – Icaro

+0

我想增加被刷過的對象我不知道用戶會刷什麼對象,但我會知道他們什麼時候在做我如何問我可以檢索該objectId時,他們刷卡嗎?我在編輯中加入了其他代碼。任何幫助將非常感激。 – DanielWolfgangTrebek