我使用自定义单元格(xib(具有表观。
此自定义单元具有图像和描述。如果用户点击图像(不是单元格(,则应显示其他页面/视图。
这是图像敲击
时称为的函数@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)
}
我尝试使用了推动视图控制器,但什么也没发生。
您可以在此处使用委托模式来处理TableViewCell中的Profile Image来推动ProfileViewController。
首先创建CellImageTappableDelegate
协议
protocol CellImageTappableDelegate: class {
func didTapProfileImage(with imageUrl: String)
}
然后在您的Customcell中创建委托属性
weak var imageTapDelegate: CellImageTappableDelegate?
现在启用isUserInteractionEnabled
属性 UIImageView
image.isUserInteractionEnabled = true
然后将tapGestureRecognizer
添加到您的ImageView并实现Selector
let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(CustomProfileCell.didTapProfileImage(_:)))
profileImageView.addGestureRecognizer(tapGestureRecognizer)
func didTapProfileImage(_ tapGesture: UITapGestureRecognizer) {
self.imageTapDelegate?.didTapProfileImage(with: self.profileImageUrl)
}
在您的 UIViewController
子类中的tableview datasource cellForRowAt:
方法中,将代表分配给 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不会有父母,这绝对不会有NavigationController。如果navigationcontroller是层次结构中的第一个ViewController,则可以尝试:
(self.window?.rootViewController as? UINavigationController)?.pushViewController(vc, animated: true)