我遇到了 UITableVIew
的问题,假设我有一个带有 tableView 的屏幕,其 resuablecell 单元格中包含三个图像,还包含一些文本,我想打开它图像作为弹出窗口以查看更大的内容,因此添加了 UITapGesture
,并且为了打开它,我将像这样传递图像引用,
@objc func showImage (_ sender: UITapGestureRecognizer){
print("ImageTapped")
let imagePopupVC: ImagePopupViewController = self.storyboard?.instantiateViewController(withIdentifier: "imagePopupVC") as! ImagePopupViewController
imagePopupVC.isImage = true
if popUpFeedImage != nil {
// here this feedImage is the one i am passing to other VC
imagePopupVC.image = popUpFeedImage
}
self.navigationController?.pushViewController(imagePopupVC, animated: false)
}
这里我在cellForRowAt
popUpFeedImage
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: MemberTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MemberTableViewCell", for: indexPath) as! MemberTableViewCell
let urlString: URL = URL(string: postContent.contentURL)!
let tap = UITapGestureRecognizer(target: self, action: #selector(showImage(_:)))
cell.feedImageView.isUserInteractionEnabled = true
cell.feedImageView.addGestureRecognizer(tap)
cell.feedImageView.sd_setImage(with: urlString, placeholderImage: nil) { (image, error, imageCacheType, url) in
self.popUpFeedImage = image
}
}
现在问题来了,当该图像已填充时,我点击第一张图像,它会打开该图像,该图像是单元格中的最后一张图像,即第三张图像,并且对于最后显示的所有图像都会发生这种情况将显示的屏幕,现在我不知道如何保存我在特定Index
上点击的图像的引用,
我已经实现了 didSelectItemAt
来打开另一个 viewController
,所以我不能将其用于我的特定目的。
任何帮助都会有帮助,谢谢。
最佳答案
您可以从手势识别器处理方法中的点击 View 获取图像:
@objc func showImage (_ sender: UITapGestureRecognizer){
print("ImageTapped")
guard let tappedView = sender.view else { return }
if let imageView = tappedView as? UIImageView {
self.popUpFeedImage = imageView.image
} else {
tappedView.subviews.forEach { subView in
if subView.isKind(of: UIImageView.self) {
self.popUpFeedImage = subView.image
}
}
}
let imagePopupVC: ImagePopupViewController = self.storyboard?.instantiateViewController(withIdentifier: "imagePopupVC") as! ImagePopupViewController
imagePopupVC.isImage = true
if popUpFeedImage != nil {
// here this feedImage is the one i am passing to other VC
imagePopupVC.image = popUpFeedImage
}
self.navigationController?.pushViewController(imagePopupVC, animated: false)
}
或者您可以为 imageView 指定标签号,当您想要获取图像时,只需比较点击的 View id 和 imageView id 即可。
关于ios - 保存特定 Index 的 tableView 数据,该 Index 是使用点击手势点击的,但不使用 didSelectItemAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288021/