【问题标题】:Implement share button in TableViewCell with Firebase data使用 Firebase 数据在 TableViewCell 中实现共享按钮
【发布时间】:2019-03-05 02:39:29
【问题描述】:

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

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    使用按钮位置获取单元格的 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)
        }
    }
    

    【讨论】:

    • 如何将 newRelease.releaseimage 设置为 image 变量?
    • 感谢您的回复。虽然收到此错误消息。无法转换“字符串”类型的值?到指定类型'UIImage'
    • 发生错误是因为您将 string 类型分配给 image 类型。如果是字符串,则从 newRelease.releaseimage 下载图像。
    • 您可以使用stackoverflow.com/questions/14902835/…从url字符串下载图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2021-04-21
    相关资源
    最近更新 更多