簡體   English   中英

使用Firebase數據在TableViewCell中實現共享按鈕

[英]Implement share button in TableViewCell with Firebase data

我正在嘗試在TableViewCell中實現使用文本和圖像數據的共享按鈕。 這段代碼可以完美地工作,但是我希望TableViewCell中的數據可以動態使用。

@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
}

使用按鈕位置獲取單元格的indexPath,然后從模型對象中提取特定數據。

編輯:

您可以在UIActivityViewController直接與圖像共享圖像,因為如果圖像位於url中,則需要先下載圖像,然后才能共享,方法是將其保存在數組中。

@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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM