【问题标题】:One click on image inside cell of UITableView一键点击 UITableView 单元格内的图像
【发布时间】:2017-04-03 15:27:31
【问题描述】:

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

    }

【问题讨论】:

  • 您的实际问题是什么?
  • 您需要双击吗?还是在您执行双击之前该行为不会发生?
  • 我需要点击一下才能移动到另一个视图而不是双击
  • 为什么要在didSelectRowAt 方法中添加点击手势?这根本没有意义。

标签: ios swift uitableview


【解决方案1】:

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

现在我会给你我的答案。我不希望我的以下答案享有声誉。

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

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 的回答。但如果你能回答,我还有一个问题。
【解决方案2】:

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

正如你所说,除了在 cellForRowAt indexPath 中添加代码之外,我建议在下面更改标签值中的图像 id:

修改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 的图像标签,所以当我使用你的方法时,标签值是最后一张图像的值,但我需要它们每个的值.
  • 非常感谢这对我有用。但如果你能帮我解决这个问题,我还有一个问题,
  • 还有什么问题?我可以试试:)
  • @AliJalil 正在下载。但是您能否快速查看设备的 iOS 版本和 iOS 部署目标的版本(您可以在应用目标的 Build Settings 下找到它)。
  • 两者都是iOS 10.2
【解决方案3】:

您正在“在索引路径处选择行”方法中分配手势识别器,这意味着用户必须选择(点击)一个单元格以将手势识别器分配给图像,然后用户必须点击那些识别器通过调用“imgTap()”做出反应的图像,这是两个水龙头。

您应该做的是在“索引路径处的行的单元格”方法中分配手势识别器,因此当您创建每个单元格时,您也会创建手势识别器,这样当用户第一次点击图像时点击被识别并调用“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()”,而不是使用 2 个方法“imgTap()”和“imgTap1()”。

【讨论】:

  • 我以前这样做过,但我想使用包含项目 id 的图像标签,所以当我使用你的方法时,标签值是最后一张图像的值,但我需要它们每个的值.如何使用 ID。
  • 在做剩下的工作之前,只需配置单元格(如评论所述),使其具有新图像和新标签,然后获取新标签。
  • 单元格配置是您当前在“索引路径处的行单元格”方法中拥有的任何内容,您是否尝试过在此方法结束时创建手势识别器?
【解决方案4】:
  1. 请不要使用强制解包as! prodctCell
  2. 请使用快速命名约定
  3. 不要使用 .tag 单元格通常会被重复使用,在某些极端情况下可能会损坏

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

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

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

更新 我建议不要在cellForRow 中添加处理点击的逻辑,viewController 并不意味着处理和管理他的子逻辑。

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多