Search code examples
iosswiftuiactivityviewcontrolleribactionnsindexpath

Error: Use of unresolved identifier 'indexPath'


I have a IBAction which opens the UIActivityViewController:

@IBAction func share(sender: UIButton) {

    let defaultText = "Check out my Pinpoint: " + self.pinpoints[indexPath.row].main + ". Get Pinpoint on the iOS App Store!"
        if let imageToShare = UIImage(data: self.pinpoints[indexPath.row].image) {
            let activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
            self.presentViewController(activityController, animated: true, completion: nil)
    }
}

The error comes in the 2nd and 3rd line (starting with let defaultText and if let imageToShare.) It says:

Use of unresolved identifier 'indexPath'

The word pinpoints is an array storing main (UILabel) and image (UIImageView.)

How can I resolve this error?


Solution

  • Just give your button a tag in cellforRowAtIndexPath which will be equal to indexPath.row

    someButton.tag = indexPath.row
    

    Now your func should be:

    IBAction func share(sender: UIButton) { 
        let defaultText = "Check out my Pinpoint: " + self.pinpoints[sender.tag].main + ". Get Pinpoint on the iOS App Store!" 
        if let imageToShare = UIImage(data: self.pinpoints[sender.tag].image) { 
            let activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
             self.presentViewController(activityController, animated: true, completion: nil) 
        } 
    }