ios - 为什么在代码中选择单元格后我的自定义 Collection View 单元格意外为零?

标签 ios swift uicollectionview

所以我有一个 colectionView 图像,当后台发生某些事情时,我可能会尝试使用以下方法选择特定的自定义 collectionViewCell:

self.collectionView.selectItemAtIndexPath(indexPathToReload, animated: true, scrollPosition: UICollectionViewScrollPosition.CenteredVertically),效果很好,collectionView 滚动到所需位置。

但是,如果我随后尝试实际更新单元格的外观,因为它已通过调用 self.collectionView(self.collectionView, didSelectItemAtIndexPath: indexPathToReload) 进行更新,当我得到一个意外的 nil 单元格时然后尝试在 didSelectItemAtIndexPath 中创建单元格。

我部分理解为什么这种更新单元格的方法不安全(正如我在研究这个问题的其他地方读到的,比如 here in one of the answers。)

因此,崩溃让我假设单元格不是屏幕上可见单元格的一部分,这就是为什么当我尝试创建单元格时单元格为零。但这没有意义,因为我还假设必须创建单元格才能滚动到,正如我所说,这很好,因为它们是按预期创建的,并且可以毫无问题地与之交互。

那么为什么我的手机是零?或者为什么我的 Collection View 不认为滚动到的单元格不是可见单元格的一部分?如果原因很明显,那么当我在代码中选择它时,如何使单元格更新它的外观?

编辑:上下文中的代码

dispatch_async(dispatch_get_main_queue()) {
    self.collectionView.selectItemAtIndexPath(indexPathToReload, animated: true, scrollPosition: UICollectionViewScrollPosition.CenteredVertically)
    self.collectionView(self.collectionView, didSelectItemAtIndexPath: indexPathToReload)
    return
}

正如我所说,这几乎就是上下文。在第一行中,我可能会滚动到屏幕上不可见的索引。如果我这样做,然后执行第二行代码,在调用的委托(delegate)方法中创建的单元格意外地为 nil。

最佳答案

为了解决这个问题,我不得不使用一种 hacky 的解决方法,尽管工作看起来几乎太脏了,而且我不知道为什么这个问题没有被 Apple 解决(即为什么 selectItemAtIndexPath 不调用委托(delegate)方法 didSelectItemAtIndexPath)。无论如何,我最终做的是当我需要在后台更新我选择的单元格时,我首先得到索引并设置一个 bool 来显示在代码中选择了一个单元格:

dispatch_async(dispatch_get_main_queue()) {
    self.collectionView.selectItemAtIndexPath(indexPathToReload, animated: true, scrollPosition: UICollectionViewScrollPosition.CenteredVertically)
    let cell = self.collectionView.cellForItemAtIndexPath(indexPathToReload) as? ListingCollectionViewCell
    if cell != nil {
        self.collectionView(self.collectionView, didSelectItemAtIndexPath: indexPathToReload)
        return
    } else {
        self.buttonSelectedInCode = true
        self.indexPathSelectedInCode = indexPathToReload
        return
    }
}

在上面,我必须尝试为指定的索引路径创建单元格。如果单元格不为零,那么我就知道该单元格是可见的,调用委托(delegate) didSelectItemAtIndexPath 是安全的。但是,如果单元格为 nil,那么我必须设置我的 bool 和索引,并等待 ScrollView 调用委托(delegate)方法,如下所示。

然后,我进一步实现了scrollViewDidEndScrollAnimation,并调用了这个委托(delegate)方法,然后在代码中选择了我的cell,如下:

func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
    if buttonSelectedInCode {
        self.collectionView(self.collectionView, didSelectItemAtIndexPath: self.indexPathSelectedInCode)
    }
}

关于ios - 为什么在代码中选择单元格后我的自定义 Collection View 单元格意外为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33512771/

相关文章:

ios - 使用 swift 3 通过键盘移动 View

ios - 上下文类型 'NSFastEnumeration' 不能与数组文字一起使用

ios - 将 collectionview 从函数传递到 Selector (UILongPressGestureRecognizer)

iOS/swift : How to implement longPressed action for backButton?

ios - 同一 App ID 下的 iPhone 和 iPad 版本

swift - 在 Apple Watch 中进行 Web 服务调用是最佳实践吗?

ios - 您将如何在 swift ios 中设计 “week view”?

Swift UICollection 单元格设置每隔一行的高度

objective-c - 如何知道 iPhone 用户 SIM 卡是否已更改?

ios - MVP: View Controller 可以向另一个 View Controller 的呈现者发送消息吗?