单击UITableView单元格内的图像

7

我在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)

    }

你的实际问题是什么? - Dima
你想要需要双击吗?还是只有在你执行双击后才会发生这种行为? - dst3p
我需要单击而不是双击即可移动到另一个视图。 - user7787622
你为什么要在 didSelectRowAt 方法中添加轻拍手势?这完全没有意义。 - rmaddy
4个回答

10

昨天我创建了一个样本并尝试了一下。我找到了解决方案。但由于有些工作要做,我不能立即发布我的答案。

现在我会把我的答案告诉你。我不指望因为下面的回答而得到声誉。

当您第一次点击或轻触图像时,它会导航。

您无需在didSelectRowAt方法中的imageView上添加TapGestureRecognizer。您需要在cellForRowAt方法中的imageView上添加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{

        }
    }
} 

输出截图

输入图像说明

打印结果如下

当您第一次单击第一张图片时

输入图像说明

然后,当您在第一次单击中单击第二张图片时

输入图像说明


谢谢,那就像@sanjana的回答一样。 但是我有另一个问题,如果您能回答的话。 - user7787622
这个不起作用。也许是苹果做了一些更改或者你忽略了某些配置?在图像上,didSelectRowAt 被调用而不是手势识别器。 - John

8

您需要在cellForRowAt indexPath中执行此代码,而不是在did select中执行。

正如您所说,您想要使用标签值中的图像ID,我建议在添加代码到cellForRowAt indexPath之外进行以下更改:

将tapGesture代码更改为以下内容:

let tapGesture = UITapGestureRecognizer (target: self, action: #selector(imgTap(tapGesture:)))

还有imgTap功能:

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

    }

我以前做过这个,但我想使用包含项目ID的图像标签,所以当我使用您的方法时,标签值是最后一个图像的值,但我需要每个图像的值。 - user7787622
还有其他问题吗?我可以尝试解决 :) - sanjana
@AliJalil 正在下载。但是您能否快速检查设备的iOS版本和iOS部署目标的版本(您可以在应用程序目标的构建设置下找到它)。 - sanjana
@AliJalil,我甚至在模拟器上都无法运行,它不停地给我随机错误 :( :( :( - sanjana
我发了封邮件给你,你能通过它进行沟通吗?:{ :( - user7787622
显示剩余3条评论

0
  1. 请勿使用强制解包 as! prodctCell
  2. 请使用 Swift 命名规范
  3. 不要使用 .tag,因为单元格通常会被重用,在某些边缘情况下可能会出现问题

现在回到你的问题,你已经有一个持有图像的自定义单元格。 你有几种可能性:

  1. 将手势识别器添加到 imageView 上,你可以将其更改为按钮
  2. 在图像视图上添加一个没有标题或图像的按钮

然后创建一个 @IBAction 用于按钮/手势识别器,并创建一个委托方法,该方法将调用主视图控制器,在那里您可以传递从单元格实例化第二个 VC 所需的所有数据

更新 我建议不要在 cellForRow 中添加处理点击的逻辑,因为视图控制器不应处理和管理其子逻辑。


0

您正在“did select row at index path”方法中分配手势识别器,这意味着用户必须选择(轻触)单元格才能将手势识别器分配给图像,然后用户必须轻触图像,以便这些识别器通过调用“imgTap()”来做出反应,这是两个轻触。

相反,您应该在“cell for row at index path”方法中分配手势识别器,这样当您创建每个单元格时,也会创建手势识别器,这样当用户第一次轻触图像时,就会识别轻触并调用“imgTap()”。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! CustomCell

    // Configure the cell...

    let tapGesture1 = UITapGestureRecognizer (target: self, action: #selector(ItemsController.imgTap))
    cell.image1st.addGestureRecognizer(tapGesture1)
    let tapGesture2 = UITapGestureRecognizer (target: self, action: #selector(ItemsController.imgTap))
    cell.image2nd.addGestureRecognizer(tapGesture2)

    return cell
}

我还建议稍微修改一下代码,这样你就可以使用参数(被点击的元素的ID)来调用“imgTap()”,而不是拥有两个方法“imgTap()”和“imgTap1()”。

我之前做过这件事,但我想使用包含项目ID的图像标签,因此当我使用您的方法时,标签值是最后一个图像的值,但我需要每个图像的值。 我该如何使用ID? - user7787622
在执行其余操作之前,只需按照注释所说的配置单元格,使其具有新图像和新标签,然后获取新标签。 - Oscar A. Esquivel Oviedo
单元格配置是您当前在“cell for row at index path”方法中拥有的内容,您尝试过在此方法结束时创建手势识别器吗? - Oscar A. Esquivel Oviedo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接