简体   繁体   中英

Extract row data using editActionsForRowAtIndexPath from Parse.com PFQueryTableViewController

I am using PFQueryTableViewController to retrieve images from Parse.com backend. Later, I want to take a snapshot of swiped row image (using editActionsForRowAtIndexPath ).

At this moment, I can retrieve object and create a action on row using editActionsForRowAtIndexPath . The action passes the retrivedObject through a prepareForSegue method to ShareViewController . Once I am on ShareViewController, I can see an image, but it is not the same I clicked to share using 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

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

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 . By setting retrivedObject = object it is always set to the last object (cell) displayed. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM