2015-05-29 52 views
0

我是iOS編碼和解析的新手,但有一個問題。我創建了一個訪問PFObjects並正確顯示它們的表視圖。我的問題是,我在每個單元格中都有一個按鈕(類似按鈕),我希望它在按下時更新特定的PFObject。我如何訪問它?從TableViewController訪問和更新PFObject

class chinTwoTimelineTableViewController: UITableViewController { 

var timelineData:NSMutableArray = NSMutableArray() 

func loadData() { 
    timelineData.removeAllObjects() 

    var findTimelineData:PFQuery = PFQuery(className: "ChinTwos") 


    findTimelineData.findObjectsInBackgroundWithBlock{ 
     (objects:[AnyObject]?, error:NSError?)->Void in 

     if let objects = findTimelineData.findObjects() as? [PFObject] { 

      if error == nil{ 
       for object in objects{ 
        let chinTwo:PFObject = object as PFObject 
        self.timelineData.addObject(chinTwo) 
       } 

       let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects 
       self.timelineData = NSMutableArray(array: array) 

       self.tableView.reloadData() 

      } 
     } 
    } 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell:ChinTwoTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ChinTwoTableViewCell 

    // Configure the cell... 

    let chinTwo:PFObject = self.timelineData.objectAtIndex(indexPath.row) as! PFObject 


    var myVar:Int = chinUp.objectForKey("likeCount") as! Int 
    cell.countLabel.text = String(myVar) 

    cell.nameLabel.text = chinUp.objectForKey("name") as? String 
    cell.bodyText.text = chinUp.objectForKey("body") as! String 

    cell.bodyText.font = UIFont(name: "HelveticaNeue-UltraLight", size: 18) 
    cell.bodyText.textAlignment = .Center 

    return cell 
} 

@IBAction func likeButtonTapped(sender: AnyObject) { 
    // This is where I am lost. 

}

如果您需要更多的信息來幫助,讓我知道,我真的很感激。

回答

0

您可以使用convertPoint獲得選定的IndexPath,然後從數組中獲取PFObject。

@IBAction func likeButtonTapped(sender: AnyObject) { 
    // This is where I am lost. 
     var senderPoint = sender.convertPoint(sender.bounds.origin, toView: self?.tableView) 
     var indexPath = self?.tableView.indexPathForRowAtPoint(senderPoint) 
     let chinTwo:PFObject = self.timelineData.objectAtIndex(indexPath.row) as! PFObject 
} 

好運

+0

太謝謝你了!這似乎訪問它。那麼我怎麼會更新呢?我想增加喜歡的數量。我嘗試了chinTwo [「likeCount」] = chinUp [「likeCount」] + 1,但是這給了我一個錯誤:Binary運算符'+'不能應用於'AnyObject'類型的操作數?和'Int' –

+0

嘗試chinTwo [「likeCount」] =(chinUp [「likeCount」]爲Int)+ 1 –

+0

YES!這樣可行。非常感謝!! –