【问题标题】:UICollectionViewCell didDeselectItemAtIndexPath not called未调用 UICollectionViewCell didDeselectItemAtIndexPath
【发布时间】:2015-10-13 23:50:11
【问题描述】:

我正在尝试创建一个集合视图,除了我尝试选择一个单元格之外,一切正常。

我的方法didDeselectItemAtIndexPath没有被调用。

在主故事板中,我在导航视图控制器中嵌入了一个 collectionviewcell 控制器。 collectionviewC调用另一个控制器打印细节,两者之间,有一个segue叫segueDetailFeature 我有另一个项目,我确实喜欢 tableviewcontroller 并且它有效。

图像显示良好,numebrofitemsinsection 中的打印返回良好的价值。

这是我的控制器的代码:



class ViewController: UICollectionViewController {


    var listImageUrl = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // code to populate the listImageUrl

        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("nombre d'image : \(listImageUrl.count)")
        return listImageUrl.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell

        let imageView = cell.viewWithTag(1) as! UIImageView
        let urlString: NSString = listImageUrl[indexPath.row] as NSString
        let imgURL: NSURL = NSURL(string: urlString as String)!

        //Télécharger une représentation NSData de l'image à cette URL
        let imgData: NSData = NSData(contentsOfURL: imgURL)!
        imageView.image = UIImage(data: imgData)

        //imageView.image = UIImage(named: listImageUrl[indexPath.row])

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        print("selected item at index")
        let urlImage = listImageUrl[indexPath.row]
        imageUri = urlImage
        performSegueWithIdentifier("segueDetailFeature", sender: collectionView)
    }


}

我找不到为什么我的代码不起作用,为什么我的didDeselectItemAtIndexPath 没有被调用。然而,当我运行它时,该应用程序使用 segue 并加载细节控制器,但它失败了,因为在方法中没有实例化一个变量确实选择。

提前致谢

【问题讨论】:

  • 您的意思是实现didSelect 而不是didDeselect
  • 我觉得我很笨。。确实我没看到它被取消选择,他们的方法名称太长了我没有想到,我会让你知道的它有效:-) thx !
  • 我大约有 20% 的时间会犯这个错误,只是偶尔会不吸取教训。

标签: ios xcode swift uicollectionview


【解决方案1】:

如果你看看这个链接:UICollectionView - didDeselectItemAtIndexPath not called if cell is selected你可以看到他收到了同样的错误。他的代码是用 Objective-C 编写的,但希望你可以将其更改为 swift。他解释说,问题在于 UICollectionView 对单元格一无所知,但正在选择单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多