简体   繁体   English

在单元格中轻按imageview时如何转到其他视图

[英]How to go to other view when tapped imageview in a cell

I have a table view using a custom cell (xib). 我有一个使用自定义单元格(xib)的表格视图。

This custom cell have an image and a description. 该自定义单元格具有图像和描述。 If user tapped the image (not the cell) , it should display other page/view. 如果用户点击图像(而不是单元格),它应该显示其他页面/视图。

this is the function called when image tapped 点击图像时调用的函数

@objc func imageTapped(){
    let sb = UIStoryboard(name: "SbIdentifier", bundle: nil)
    let vc = sb.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
    print("image tapped")
    //self.window?.rootViewController?.parent?.navigationController?.pushViewController(vc, animated: true)
 }

I tried used the push view controller but nothing happened. 我尝试使用推式视图控制器,但没有任何反应。

You can use delegate pattern here to handle tap on profile image in tableViewCell to push ProfileViewController. 您可以在此处使用委托模式来处理对tableViewCell中的个人资料图像的点击,以推送ProfileViewController。

First create the CellImageTappableDelegate protocol 首先创建CellImageTappableDelegate协议

protocol CellImageTappableDelegate: class {
    func didTapProfileImage(with imageUrl: String)
}

then create the delegate property in Your CustomCell 然后在您的CustomCell中创建委托属性

weak var imageTapDelegate: CellImageTappableDelegate?

now enable isUserInteractionEnabled property on UIImageView 现在在UIImageView上启用isUserInteractionEnabled属性

image.isUserInteractionEnabled = true

then add the tapGestureRecognizer to your imageView and implement the selector 然后将tapGestureRecognizer添加到您的tapGestureRecognizer并实现选择器

let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(CustomProfileCell.didTapProfileImage(_:)))
        profileImageView.addGestureRecognizer(tapGestureRecognizer)

func didTapProfileImage(_ tapGesture: UITapGestureRecognizer) {
     self.imageTapDelegate?.didTapProfileImage(with: self.profileImageUrl)
}

in tableView datasource cellForRowAt: method in your UIViewController subclass assign the delegate to self and implement the protocol method 在tableView数据源cellForRowAt:中,您的UIViewController子类中的方法将委托分配给self并实现协议方法

cell.imageTapDelegate = self

func didTapProfileImage(with imageUrl: String) {
   print("Profile image tapped")
   let storyboard = UIStoryboard(name: "SbIdentifier", bundle: nil)
   let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
   profileVC.imageUrl = imageUrl
   self.navigationController?.pushViewController(profileVC, animated: true)
}

RootViewController would not have a parent, which would definitely not have a navigationController. RootViewController没有父级,后者肯定没有NavigationController。 If the navigationController is the first viewController in your hierarchy, you can try: 如果navigationController是层次结构中的第一个viewController,则可以尝试:

(self.window?.rootViewController as? UINavigationController)?.pushViewController(vc, animated: true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 当点击表视图内的元素时,如何转到另一个视图控制器? - How to go to another View Controller when an element inside table view is being tapped? 如何在点击按钮时全屏显示.sheet() 以便将 go 显示到 SwiftUI 中的下一个视图? - How to present .sheet() full screen when a button is tapped in order for it to go to the next view in SwiftUI? 使用shouldPerformSegueWithIdentifier时,必须轻按两次按钮才能进入下一个视图 - Button must be tapped twice to go next view when using shouldPerformSegueWithIdentifier 使用 SwiftUI 点击该通知时,如何重定向到列表中单元格的详细视图? - How to redirect to detail view of cell in List when tapped on that notification using SwiftUI? 当我们点击它时,如何更改添加在表格视图单元格上的按钮背景色? - How to change button background color which is added on table view cell when we tapped on it? 当UITapGestureRecognizer在prepareForSegue中点击并捕获其中的UIImage时,如何获取自定义表格视图单元格的索引路径 - How to get the indexpath of a custom table view cell when an UIImage inside of it is tapped and captured by UITapGestureRecognizer in prepareForSegue 双击集合视图单元格时如何使图像可见? - How do I make image visible when collection view cell is double tapped? 当我们在Ios中点击它时,如何使用图像进行集合视图单元格“放大”和“缩小” - How to do collection view cell “zoom in” and “zoom out” with image when we tapped on it in Ios 轻触单元格时,表格视图单元格会更改高度 - Table View Cells change Height when cell tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM