通过点击单元格内的图像,从 UITableViewCell 转场 [英] Segue from UITableViewCell by tapping on an image inside of cell

查看:29
本文介绍了通过点击单元格内的图像,从 UITableViewCell 转场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解决这个问题已经有一段时间了,经过几个小时的寻找解决方案后,我决定是时候提出问题了.

Been trying to figure this out for a while now and after a couple hours of searching for a solution I decided it was time to ask.

我有一个由自定义 UITableViewCells 填充的 tableview,目前当您点击一个单元格时,它会将您带到详细视图.

I have a tableview that gets populated by custom UITableViewCells and currently when you tap on a cell it takes you to a detail view.

在自定义单元格中有一个图像,我希望用户能够点击该图像并转到显示该图像的 popover VC.

Within the custom cell there is an image, I would like the user to be able to tap on that image and segue to a popover VC that shows the image.

我遇到的问题是在点击图像时创建转场.

What I'm having trouble with is creating the segue when the image is tapped.

在自定义单元格的文件中,我在图像上设置了点击手势识别器(pTap):

In the file for the custom cell, I've set up a tap gesture recognizer on the image (pTap):

override func awakeFromNib() {
    super.awakeFromNib()

    let tap = UITapGestureRecognizer(target: self, action: #selector(PostCell.voteTapped(_:)))
    let ptap = UITapGestureRecognizer(target: self, action: #selector(PostCell.imageTapped(_:)))
    tap.numberOfTapsRequired = 1
    ptap.numberOfTapsRequired = 1
    voteImage.addGestureRecognizer(tap)
    voteImage.userInteractionEnabled = true
    featuredImg.addGestureRecognizer(ptap)
    featuredImg.userInteractionEnabled = true


}

我在自定义单元格文件中还有一个用于点击的函数:

I also have a function in the custom cell file for the tap:

func imageTapped(sender: UIGestureRecognizer) {

  print("image tapped")


}

在我的视图控制器文件中,我添加了一个 segue in did select row at index path:

In my view controller file I've added a segue in did select row at index path:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let post: Post!

    if inSearchMode {
        post = filteredVenues[indexPath.row]
    } else {

        post = posts[indexPath.row]
    }

    print(post?.venueName)

    performSegueWithIdentifier("imageTapped", sender: nil)



    performSegueWithIdentifier("DetailsVC", sender: post)

}

此外,在情节提要中,我从 VC 创建了一个 segue,将带有自定义单元格的 tableview 保存到我想在点击图像时显示的 VC.

Also, in the storyboard I have created a segue from the VC that holds the tableview with the custom cells to the VC I'd like to show when the image is tapped.

我尝试了几种不同的方法来让它工作,但没有任何运气,你在上面看到的代码是我多次尝试失败后剩下的.我觉得自定义单元格文件中的 tap 函数和 VC 文件中的 segue 是解决方案的一部分,所以这就是我保留它们的原因.

I've tried several different methods of getting this to work and haven't had any luck, the code you see above are what remains after my many failed attempts. I feel that the function for the tap in the custom cell file and the segue in the VC file are a part of the solution so that is why I have left them in.

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

更新以下答案中的代码:

Updates to code from answers below:

添加协议

protocol ImageSegueProtocol: class {
    func imageTapped(row: Int)
}


class PostCell: UITableViewCell {

添加了 IAB 功能

@IBAction func imageTapped(sender: UIGestureRecognizer) {
   guard let row = row else { return }
    delegate?.imageTapped(row)
    print("image tapped func")
}

在主 VC 中声明的委托

Declared delegate in the Main VC

weak var delegate:postCell?

指定的代言人

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //let post = posts[indexPath.row]

    if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {

        var img: UIImage?
        var vImg: UIImage?
        postCell?.delegate = self

新增扩展功能

extension FeedVC: ImageSegueProtocol {

func imageTapped(row: Int) {
    if inSearchMode == true {
        let object = filteredVenues[row]
        performSegueWithIdentifier("imageTapped", sender: object)
        print("ext func")
    } else {
        let object = posts[row]
        performSegueWithIdentifier("imageTapped", sender: object)
        print("ext func")
    }



}

推荐答案

你可以这样做:

在单元文件中,在顶部声明一个协议,然后在单元类本身中设置一些属性以及响应点击图像的委托行为:

Within the cell file, declare a protocol at the top, and then set up some properties within the cell class itself and the delegate behaviour in response to the image being tapped:

protocol SomeCellProtocol: class {
    func imageTapped(row: Int)
}


class SomeCell: UITableViewCell {

    weak var delegate: SomeCellProtocol?
    var row: Int?

    @IBAction func imageTapped() {
        guard let row = row else { return }
        delegate?.imageTapped(row)
    }
}

然后你必须让你的 ViewController 采用 SomeCellProtocol 并像这样调用 segue:

You must then make your ViewController adopt SomeCellProtocol and call the segue within like so:

extension SomeViewController: SomeCellProtocol {

    func imageTapped(row: Int) {
        //get object from array and call segue here
    }
}

并且您必须通过调用将 ViewController 设置为单元格的委托:

And you must set your ViewController as the delegate of your cells by calling:

someCell.delegate = self

并将行传递给单元格:

someCell.row = indexPath.row

在 ViewController 的 cellForRowAtIndexPath 方法中.

within your cellForRowAtIndexPath method of the ViewController.

因此,当在单元格内点击按钮时(或者您可以使用 ImageView 上的 GestureRecognizer,如果您愿意),它将强制委托(ViewController)调用其 imageTapped 函数,传递一个行参数,您可用于确定应通过 Segue 传递表中的哪个对象(其对应的数据数组).

So, when the button is tapped within the cell (or you can do it with a GestureRecognizer on the ImageView if you want) it will force the delegate (the ViewController) to call its imageTapped function, passing a row parameter which you can use to determine which object in the table (its corresponding data array) should be passed via the Segue.

这篇关于通过点击单元格内的图像,从 UITableViewCell 转场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆