一击UITableView单元格内的图像

5 uitableview ios swift

我在uitableview的一个单元格内有两个图像,这些图像显示到来自外部服务器的图像,并且它们的每个标签都有该图像表示的项目ID,如果我单击此图像,则需要将用户移动到新的视图控制器,该图像显示此项的详细信息,我遇到了一个问题,即用户需要双击以下代码来显示详细信息,而不是单击以显示详细信息:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = self.tableView.cellForRow(at: indexPath as IndexPath) as! prodctCell
        Id1stMove =  cell.image1st.tag
        let tapGesture = UITapGestureRecognizer (target: self, action: #selector(ItemsController.imgTap))
        cell.image1st.addGestureRecognizer(tapGesture)
        cell.image1st.isUserInteractionEnabled = true

        let cell1 = self.tableView.cellForRow(at: indexPath as IndexPath) as! prodctCell
        Id2ndMove =  cell1.image2nd.tag
        let tapGesture1 = UITapGestureRecognizer (target: self, action: #selector(ItemsController.imgTap1))
        cell1.image2nd.addGestureRecognizer(tapGesture1)
    }


func imgTap()
    {
        let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as? testViewController
            let navController = UINavigationController(rootViewController: secondViewController!)
            navController.setViewControllers([secondViewController!], animated:true)
            self.revealViewController().setFront(navController, animated: true)
            revealViewController().pushFrontViewController(navController, animated: true)
        secondViewController?.movmentId = Id1stMove
        updateCount(itemId: Id1stMove)

    }
Run Code Online (Sandbox Code Playgroud)

san*_*ana 6

您需要在cellForRowAt indexPath中执行此代码,而不是选择了。

如您所说,您想使用标记值中的图片ID,除了在cellForRowAt indexPath中添加代码外,我还建议进行以下更改:

更改tapGesture代码,如下所示:

let tapGesture = UITapGestureRecognizer (target: self, action: #selector(imgTap(tapGesture:)))
Run Code Online (Sandbox Code Playgroud)

和imgTap函数:

 func imgTap(tapGesture: UITapGestureRecognizer) {
        let imgView = tapGesture.view as! UIImageView
        let idToMove = imgView.tag
       //Do further execution where you need idToMove

    }
Run Code Online (Sandbox Code Playgroud)


use*_*143 6

昨天我自己创建了示例并进行了尝试。得到了解决方案。但是由于做一些工作,我无法立即发布答案。

现在我给你答案。我不希望我的以下答案声名远播。

首次单击或点击图像时,它会导航。

您无需在didSelectRowAt方法中为imageView添加TapGestureRecognizer,而需要在cellForRowAt方法中为图像View添加TapGestureRecognizer。

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    let mobiles: [String] = ["iPhone", "Android"]
    let images: [String] = ["iPhone.png", "android.png"]
    @IBOutlet var tableViewImageTapping: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // number of rows in table view
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.mobiles.count
    }

    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell")
        if (cell == nil) {
            cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:"cell")
        }
        cell?.textLabel?.text = self.mobiles[indexPath.row]
        let strImageName = images[indexPath.row]
        cell?.imageView?.image = UIImage(named: strImageName)
        cell?.imageView?.tag = indexPath.row
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
        tapGestureRecognizer.numberOfTapsRequired = 1
        cell?.imageView?.isUserInteractionEnabled = true
        cell?.imageView?.addGestureRecognizer(tapGestureRecognizer)
        return cell!
    }

    // method to run when table view cell is selected
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped table view cell index is \(indexPath.row).")
    }

    // method to run when imageview is tapped
    func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        let imgView = tapGestureRecognizer.view as! UIImageView
        print("your taped image view tag is : \(imgView.tag)")
        if (imgView.tag == 0) //Give your image View tag
        {
            //navigate to next view
        }
        else{

        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

输出截图

在此处输入图片说明

打印结果是

当您单击第一个单击的第一张图像时

在此处输入图片说明

然后,当您单击第二个图像时,首先单击

在此处输入图片说明