【问题标题】:why are my collectionViewCell's displaying unusual behavior?为什么我的 collectionViewCell 显示异常行为?
【发布时间】:2015-09-05 21:24:03
【问题描述】:

由于某种原因,我的 collectionViewCell 在被选中时无法识别。直到之后触摸另一个单元格时,才会识别前一个单元格。为了解释我是如何实现这一点的,我将以下代码添加到我的 collectionView 的 didDeselectItemAtIndexPath 方法中:println("user tapped on cell # \(indexPath.row)")。当我运行应用程序并选择一个单元格时,我的控制台不会响应,直到我点击另一个单元格,然后它会读取我添加的 println。例如,如果我选择第一个单元格,调试器不会打印任何内容,直到我选择另一个单元格,然后控制台读取“user tapped on thumbnail # 0”。

现在我已经在我的 collectionView 中添加了一个动画,它会在选择时放大每个单元格,所以这就是我知道这不是 indexPath 问题的原因,因为控制台中打印的单元格 indexPath # 是正确的单元格在视图中放大,但就像我说的那样,单元格在其选择时没有动画,直到我之后选择另一个单元格。

这是我的 collectionView 委托和数据源逻辑:

 // MARK: UICollectionViewDataSource
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 1
    }


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

        return self.books.count
    }

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

        // Configure the cell
        let book = self.books[indexPath.row]
        let coverImage = book.coverImage
        if coverImage == nil {
            book.fetchCoverImage({ (image, error) -> Void in
                if self.collectionView != nil {

                    collectionView.reloadItemsAtIndexPaths([indexPath])
                }
            })
        } else {
            let imageView = cell.imageView
            imageView.image = book.coverImage
        }

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

        let book = self.books[indexPath.row]

        self.selectedImageView = cell.imageView

        if !isModeModal {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController

            controller.imageSelected = book.coverImage

            self.navigationController?.pushViewController(controller, animated: true)

        }

        println("user tapped on thumbnail # \(indexPath.row)")

    }
}

为什么会出现这种情况?

【问题讨论】:

  • 仔细看你已经实现的方法的名称 didDeselectItemAtIndexPath` - 你想要didSelectItemAtIndexPath
  • 天哪,这很尴尬哈哈@Paulw11怎么可能错过了
  • 自动补全的风险...
  • 完全是哈哈@Paulw11
  • 我们都去过那里。

标签: ios xcode swift uicollectionview uicollectionviewcell


【解决方案1】:

我没有运行您的代码,但我认为问题可能是由didDeselectItemAtIndexPath 引起的。尝试改用didSelectItemAtIndexPath

如有需要可以加deselectItemAtIndexPath

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.deselectItemAtIndexPath(indexPath: NSIndexPath, animated: Bool)

}

【讨论】:

  • 哦,天哪,我只是偶然发现它是 didDeselectItemAtIndexPath :(((((((((..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 2011-04-08
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多