0

I’m trying to implement share button in TableViewCell that uses data for the text and image. This code works perfect but I want the data from the TableViewCell to be used dynamically.

@IBAction func tapShareButton(_ sender: UIButton) {
     //Shoename should be the releasename
    let firstActivityItem = "Find out everywhere the *shoename* is available to purchase"
    let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
    // Image should be the releaseimage
    let image : UIImage = UIImage(named: "image.jpg")!

    let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)

    activityViewController.popoverPresentationController?.sourceView = (sender )
            activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)

    // Anything you want to exclude
    activityViewController.excludedActivityTypes = [
        UIActivity.ActivityType.postToWeibo,
        UIActivity.ActivityType.print,
        UIActivity.ActivityType.assignToContact,
        UIActivity.ActivityType.saveToCameraRoll,
        UIActivity.ActivityType.addToReadingList,
        UIActivity.ActivityType.postToFlickr,
        UIActivity.ActivityType.postToVimeo,
        UIActivity.ActivityType.postToTencentWeibo
    ]

    self.presentViewController(activityViewController, animated: true, completion: nil)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ReleaseCell", for: indexPath) as! ReleaseTableViewCell
    var release: ReleaseModel
    release = releasesData[indexPath.row]

    cell.releaseType.text = release.releasetype
    cell.releaseName.text = release.releasename
    cell.releaseDate.text = release.releasedate
    cell.releaseImage.sd_setImage(with: URL(string: release.releaseimage!), placeholderImage: UIImage(named: "placeholder1"))

    cell.delegate = self
    return cell
}

1 Answers1

0

Get the indexPath of the cell using the button location and then extract the particular data from the model object.

Edit:

You can share the image directly along with text in UIActivityViewController, for that you need to download the image before sharing if its in url else you can share it directly by keep it in an array.

@IBAction func tapShareButton(_ sender: UIButton) {

    //Get the button position in the tableView
    let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)

    //Find the indexPath of tableView from the button position
    if let indexPath = self.tableView.indexPathForRow(at:buttonPosition) {

        //Extract release from data at particular indexPath
        let newRelease = releasesData[indexPath.row]

        //Shoename should be the releasename
        let firstActivityItem = newRelease.releasename///"Find out everywhere the *shoename* is available to purchase"


        let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
        // Image should be the releaseimage
        let image : UIImage = newRelease.releaseimage

        let activityViewController : UIActivityViewController = UIActivityViewController(
            activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)

        activityViewController.popoverPresentationController?.sourceView = (sender )
        activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)

        // Anything you want to exclude
        activityViewController.excludedActivityTypes = [
            UIActivity.ActivityType.postToWeibo,
            UIActivity.ActivityType.print,
            UIActivity.ActivityType.assignToContact,
            UIActivity.ActivityType.saveToCameraRoll,
            UIActivity.ActivityType.addToReadingList,
            UIActivity.ActivityType.postToFlickr,
            UIActivity.ActivityType.postToVimeo,
            UIActivity.ActivityType.postToTencentWeibo
        ]

        self.present(activityViewController, animated: true, completion: nil)
    }
}
Sateesh Yemireddi
  • 4,289
  • 1
  • 20
  • 37