我正在将此GSImageViewerController吊舱用于imageview点按手势,该操作正在viewcontroller中工作
但是我需要为表格单元格图像添加手势,所以在这里我遇到了错误:
总代码:
import UIKit
import GSImageViewerController
class tableviewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var imageView: UIImageView!
var nameArray = ["hewjkew", "uhrueihriu", "jfkejfkefn", "ueyduieh", "uydhuegfhejh"]
var imageArray = ["fla.png","fla.png","fla.png","fla.png","fla.png"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
@objc func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
cell.textLabel?.text = nameArray[indexPath.row]
cell.imageView?.image = UIImage(named: imageArray[indexPath.row])
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
cell.imageView?.isUserInteractionEnabled = true
cell.imageView?.addGestureRecognizer(tapGestureRecognizer)
return cell
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)
let transitionInfo = GSTransitionInfo(fromView: (cell.imageView)!)
let imageViewer = GSImageViewerController(imageInfo: imageInfo, transitionInfo: transitionInfo)
present(imageViewer, animated: true, completion: nil)
}
}
在此行let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)
中的感叹号处,我的错误率低于错误值
线程1:致命错误:展开一个可选值时意外发现nil
请解决此问题。请提供代码帮助。
答案 0 :(得分:1)
您不应使用
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
在cellForRowAt
之外(您在imageTapped
中使用过)并替换
let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)
使用
let imageV = tapGestureRecognizer.view as! UIImageView // access the current clicked image cell
let imageInfo = GSImageInfo(image:imageV.image!, imageMode: .aspectFit)
cell.imageView?.image!
对于出队单元格完全为零
答案 1 :(得分:1)
这里:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
let imageInfo = GSImageInfo(image: (cell.imageView?.image!)!, imageMode: .aspectFit)
您正在访问出列表视图单元格的image
。通常,仅在需要新单元时才使可重复使用的单元出队。 dequeueReusableCell
会为您提供一个未使用的现有单元格,或者如果“队列”为空,则会为您创建一个新单元格。碰巧它会为您创建一个新单元格,并且对于一个新单元格,其图像视图中没有图像!
您应该获取所点击的单元格图像视图的图像。由于您将手势识别器直接添加到图像视图中,因此可以通过tapGestureRecognizer.view
来访问它:
guard let image = (tapGestureRecognizer.view as? UIImageView).image else { return }
let imageInfo = GSImageInfo(image: image, imageMode: .aspectFit)
// ...
此外,由于表单元已重用,因此在添加新的手势识别器之前,应删除所有旧的手势识别器:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
cell.textLabel?.text = nameArray[indexPath.row]
cell.imageView?.image = UIImage(named: imageArray[indexPath.row])
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
// Add this line!
cell.imageView?.gestureRecognizers?.removeAll()
cell.imageView?.isUserInteractionEnabled = true
cell.imageView?.addGestureRecognizer(tapGestureRecognizer)
return cell
}