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

标签 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
}

最佳答案

使用按钮位置获取单元格的索引路径,然后从模型对象中提取特定数据。

编辑:

您可以在 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 - 使用 Firebase 数据在 TableViewCell 中实现共享按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994632/

相关文章:

ios - 我怎样才能为一个 viewController iOS 禁用横向

swift - DispatchSourceTimer 重复时间后如何执行

ios - 'NSDate' 没有可见的@interface 声明选择器 'initWithString:'

xcode - 如何使 Codeblocks 在 macOS big sur 上运行?

ios - iPhone 上的 LandScape 方向无法正常工作

ios - CGAffineTransformRotate 在执行旋转之前移动对象

ios - 如何在 PDFView 中禁用选择?

SwiftUI - 基于设备的自定义 TabBar 高度

swift - 从类名创建一个 swift2 类

iOS 测试套件 - 添加整个项目源进行测试