我有一个包含UICollectionView的ViewController,其中每个单元格都是一个自定义单元格。我需要执行一个序列,所以当用户点击任意一个单元格时,将显示一个新的ViewController来显示所按单元格的详细信息。
使用我现在拥有的代码,如果我用两根手指压在一个单元格上而不是仅用一根手指压过一个单元,则执行segue。而且,当显示DetailsViewController时,标题不会更新。
我无法发布图像,但是我在情节提要中创建的序列从单元格转到DetailsViewController,类型为Show(例如Push),id为showDetail。
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib.init(nibName: "MovieCell", bundle: nil), forCellWithReuseIdentifier: "MovieCell")
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "showDetail") {
guard let viewController = segue.destination as? DetailsViewController else {return}
guard let indexPath = sender as? IndexPath else {return}
viewController.movie = self.moviesList[indexPath.row]
}
}
DetailsViewController.swift
class DetailsViewController: UIViewController {
var movie: Movie? = nil {
didSet {
navigationController?.title = movie?.title
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
有什么想法吗?
答案 0 :(得分:0)
将发件人更改为collectionView单元:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: MovieCell)
}
使用它来准备segue:
if(segue.identifier == "showDetail") {
guard let viewController = segue.destination as? DetailsViewController else {return}
let cell = sender as MovieCell
guard let indexPath = self.collectionView!.indexPathForCell(cell) else {return}
viewController.movie = self.moviesList[indexPath.row]
}