从TableViewController访问和更新PFObject

时间:2015-05-29 22:28:30

标签: ios swift parse-platform

我是iOS编码和Parse的新手,但有一个问题。我创建了一个表视图,可以访问PFObject并正确显示它们。我的问题是我在每个单元格中都有一个按钮(类似按钮),我希望它在按下时更新特定的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. 

}

如果您需要更多信息来帮助,请告诉我,我真的很感激。

1 个答案:

答案 0 :(得分: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
}
祝你好运