首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从presentViewController中调用UICollectionViewCell

如何从presentViewController中调用UICollectionViewCell
EN

Stack Overflow用户
提问于 2016-10-18 15:03:09
回答 2查看 5.7K关注 0票数 3

UIViewController调用此函数不会造成任何问题,但从UICollectionViewCell调用该函数会引发预编译错误。

函数:

代码语言:javascript
复制
func didTapShare(sender: UIButton)
{
    let textToShare = "Swift is awesome!  Check out this website about it!"

    if let myWebsite = NSURL(string: "http://www.google.com/")
    {
        let objectsToShare = [textToShare, myWebsite]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]

        activityVC.popoverPresentationController?.sourceView = sender
        self.presentViewController(activityVC, animated: true, completion: nil)
    }
}

错误:

您的单元没有成员presentViewController。

该怎么办呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-18 15:12:11

UITableViewCell不应该处理任何业务逻辑。它应该在视图控制器中实现。您应该使用委托:

UICollectionViewCell子类:

代码语言:javascript
复制
protocol CustomCellDelegate: class {
    func sharePressed(cell: MyCell)
}

class CustomCell: UITableViewCell {
    var delegate: CustomCellDelegate?

    func didTapShare(sender: UIButton) {
        delegate?.sharePressed(cell: self)
    }
}

ViewController:

代码语言:javascript
复制
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    //...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell
        cell.delegate = self
        return cell
    }
}

extension TableViewController: CustomCellDelegate {
    func sharePressed(cell: CustomCell) {
        guard let index = tableView.indexPath(for: cell)?.row else { return }
        //fetch the dataSource object using index
    }
}
票数 16
EN

Stack Overflow用户

发布于 2016-10-18 15:33:38

这是因为presentViewController是一个UIViewController方法,UITableViewCell没有一个名为presentViewController的方法。

该怎么办呢?

您可以使用委派模式来处理按钮操作的访问(作为@alexburtnik应答),或者使用-for来节省一些额外的工作--我建议通过tag对其进行识别,从而在viewController中处理单元格按钮的操作。

注: Swift 3代码。

代码语言:javascript
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

        cell.myButton?.tag = indexPath.row
        cell.myButton?.addTarget(self, action: #selector(), for: .touchUpInside)

        return cell
    }

    func namesIsTapped(tappedButton: UIButton) {
        // get the user (from users array for example) by using the tag, for example:
        let currentUser = users[tappedButton.tag]

        // do whatever you want with this user now...

    }

希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40111778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档