ios - 在TableViewCell中使用Firebase数据实现共享按钮

原文 标签 ios swift xcode

我正在尝试在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)
    }
}

关于ios - 在TableViewCell中使用Firebase数据实现共享按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994632/

相关文章:

ios - 如何在Swift中覆盖Core Data属性

swift - 在 Windows 10 上的 Ubuntu 上的 bash 上安装 swift 4

ios - 当开发人员更改应用程序名称时,旧的应用程序名称是否可用?

ios - Xcode png图片导入及代码实现

ios - 正文中的AFNetworking GET参数

ios - "ԅ"导致 iOS 应用程序崩溃

ios - UNUserNotificationCenter didReceive/willPresent未触发ios 11

iPhone 崩溃日志 - 是什么原因?

ios - Xcode iOS:如何使用AVFoundation播放声音后重设声音

ios - 订阅Observable时如何在onNext情况下获取状态代码