【问题标题】:UICollectionViewCell Segue to new ViewControllerUICollectionViewCell Segue 到新的 ViewController
【发布时间】:2018-10-03 21:14:38
【问题描述】:

我正在尝试从 UICollectionViewCell 转移到新的 ViewController。我创建了一个从一个 ViewController 到另一个的 segue,并在我选择单元格时创建了下面的代码。

class FilmsViewController: UIViewController, UICollectionViewDelegate, 
UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, 
UISearchBarDelegate {

var films: [NSDictionary]?
var filteredFilms: [NSDictionary]?

let searchBar = UISearchBar()

//let searchController = UISearchController(searchResultsController: nil)

@IBOutlet var filmsTable: UICollectionView!

@IBOutlet var navLogo: UINavigationItem!

@IBOutlet var search: UIBarButtonItem!
@IBOutlet var back: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()


    self.filmsTable.dataSource = self
    self.filmsTable.delegate = self

    loadFilms()

}

func collectionView(_ _collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return filteredFilms?.count ?? 0

}

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = filmsTable.dequeueReusableCell(withReuseIdentifier: "filmCell", for: indexPath) as! FilmCell

            let film = filteredFilms![indexPath.row]
            let title = film["title"] as! String

            cell.titleLabel.text = title


        return cell
    }



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: NSIndexPath) {

    let film = filteredFilms![indexPath.row]
    performSegue(withIdentifier: "detailsSegue", sender: film)

}

问题是当我选择单元格时没有任何反应。没有错误,只是没有响应单击任何单元格。我认为问题是单元格没有响应被点击,但我不知道为什么。

编辑:我也试过了:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: NSIndexPath) {

    performSegue(withIdentifier: "detailsSegue", sender: FilmCell)

}

FilmCell 是 UICollectionViewCell

FilmCell 类:

class FilmCell: UICollectionViewCell {


@IBOutlet var posterImage: UIImageView!

@IBOutlet var titleLabel: UILabel!

override func awakeFromNib() {

}

}

【问题讨论】:

  • 您确定您的 segue 标识符在情节提要中设置为“detailsS​​egue”吗?并且您的发件人应该是自己而不是电影而不是 FilmCell
  • 它也不适用于 self 。细胞似乎根本没有对触摸做出反应。我还尝试从 ViewController 和 Cell 设置 segue。
  • 情节提要中的转场标识符。设置好了吗?如果您设置为单元格或胶片,发件人仍然可以工作。这只是不好的做法
  • 只需在函数中设置一个print("hello! just didSelectItemAt !"),看看它是否会出现在您的日志中。这样我们就不必猜测您是否设置了委托或...
  • @GaloTorresSevilla 是的,它设置为“detailsS​​egue”。

标签: ios swift uicollectionview segue uicollectionviewcell


【解决方案1】:

试试这个:

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

             performSegue(withIdentifier: "detailsSegue", sender: nil)
    }

【讨论】:

  • 试过了,结果一样。问题似乎是collectionview或cell没有响应触摸。
【解决方案2】:

我有一个用于退出连接到集合视图的键盘的 UITapGesture。我 100% 以上确定我已经尝试删除手势及其代码。显然我没有完全删除它,因为我回去再试一次,这解决了这个问题。集合视图内的点按手势位于单元格上方,导致触摸时无法选择它们。

解决方案是,在您的 UICollectionView 中不要有 UITapGesture。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多