[英]Extract row data using editActionsForRowAtIndexPath from Parse.com PFQueryTableViewController
I am using PFQueryTableViewController
to retrieve images from Parse.com backend. 我正在使用
PFQueryTableViewController
从Parse.com后端检索图像。 Later, I want to take a snapshot of swiped row image (using editActionsForRowAtIndexPath
). 稍后,我想拍摄滑动行图像的快照(使用
editActionsForRowAtIndexPath
)。
At this moment, I can retrieve object and create a action on row using editActionsForRowAtIndexPath
. 此时,我可以使用
editActionsForRowAtIndexPath
检索对象并在行上创建操作。 The action passes the retrivedObject
through a prepareForSegue
method to ShareViewController
. 该操作将
retrivedObject
通过prepareForSegue
方法传递给ShareViewController
。 Once I am on ShareViewController, I can see an image, but it is not the same I clicked to share using editActionsForRowAtIndexPath
. 进入ShareViewController后,我可以看到图像,但与单击使用
editActionsForRowAtIndexPath
共享的editActionsForRowAtIndexPath
。 It is either the image above or below or sometimes its the same one that i clicked. 它是上方或下方的图像,或者有时是我单击的图像。
Can anyone help me to solve this? 谁能帮我解决这个问题?
My code is as below: 我的代码如下:
PFQueryTableViewController PFQueryTableViewController
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as! DataViewTableViewCell!
if cell == nil {
cell = DataViewTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
}
retrivedObject = object
// Display profile image
let profileThumbnail = UIImage(named: "DProfilePicture")
cell.profilePicture.image = profileThumbnail
if let thumbnail = object?["profilePicture"] as? PFFile {
cell.profilePicture.file = thumbnail
cell.profilePicture.loadInBackground()
}
// Display main image
let initialThumbnail = UIImage(named: "DefaultImage")
cell.picture.image = initialThumbnail
if let thumbnail = object?["picture"] as? PFFile {
cell.picture.file = thumbnail
cell.picture.loadInBackground()
}
//........
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
print("EDIT MODE INDEX: \(indexPath.item)")
let Share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
print("Share button tapped")
self.performSegueWithIdentifier("ShareImage", sender: self)
}
Share.backgroundColor = UIColor.blackColor()
return [Share]
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.destinationViewController is ShareViewController {
let vc = segue.destinationViewController as! ShareViewController
vc.selectedObject = retrivedObject
}
}
ShareViewController ShareViewController
var selectedObject: PFObject!
var sharedImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
if selectedObject == nil {
return
}
if let file = selectedObject["picture"] as? PFFile {
file.getDataInBackgroundWithBlock({ (data, error) -> Void in
if data != nil {
self.sharedPicture.image = UIImage(data: data!)
self.screenShotMethod()
}
})
}
}
@IBAction func cancelBtn(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func shareBtn(sender: AnyObject) {
let vc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
vc.setInitialText("Look at this great picture!")
vc.addImage(sharedImage)
self.presentViewController(vc, animated: true, completion: nil)
}
func screenShotMethod() -> UIImage {
//Create the UIImage
UIGraphicsBeginImageContextWithOptions(self.shareView.frame.size, true, 2.0 )
self.shareView.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.shareView.bounds.width, height: self.shareView.bounds.height), afterScreenUpdates: false)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
sharedImage = image
return sharedImage
}
The problem is your use of retrivedObject
. 问题是您对
retrivedObject
的使用。 By setting retrivedObject = object
it is always set to the last object (cell) displayed. 通过设置
retrivedObject = object
它始终设置为显示的最后一个对象(单元格)。 It is not related to the user selection. 它与用户选择无关。
When you create the share action you should set 创建共享操作时,应设置
self.performSegueWithIdentifier("ShareImage", sender: index)
so that in the prepareForSegue
function you can use the index
(which is the sender
) to get the true selected object. 因此,在
prepareForSegue
函数中,您可以使用index
(即sender
)来获取真正的所选对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.