Yar*_*neo 6 ios uicollectionview uicollectionviewcell ios9 xcode7
自从我搬到新的iOS9和Xcode 7后,我偶然发现了UICollectionView我的应用程序中的一个问题.
显然,UICollectionView似乎没有UICollectionViewCell正确地更新布局和约束,只有在它被重用之前.
图片说得比文字更好 - 这UIViewController是第一次看到时的样子:

然而,这不是正确的布局,当我UICollectionView向左水平滑动时,我得到了新出现的单元格的正确布局:

当我向后滑动时,旧的细胞不正确,现在重复使用并且看起来很好.
现在,就像升级到iOS9和Xcode 7之前一样,我想要的效果是即使首次出现,单元格也具有正确的布局.
为方便起见,以下是有关如何UICollectionView设置及其在XIB中的约束的更多详细信息:

在代码中,它非常标准:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : MatchmakersCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! MatchmakersCollectionViewCell
cell.imageView.backgroundColor = UIColor.lightGrayColor()
cell.bottomName.text = "StackOverflow"
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.matchmakers.count
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
Run Code Online (Sandbox Code Playgroud)
每次我更新数据源(self.matchmakers)时,我都会打电话 self.collectionView.reloadData()
我注意到的最后一件事非常奇怪,当使用Xcode调试调试视图层次结构时,UICollectionViewCell从未正确显示子视图,只是给了我一个默认值UIView:

要解决这个问题:
使用创建自定义UICollectionViewCell Nib(.xib)文件.
在您的自定义UICollectionViewCell中,重写didMoveToSuperView()方法以添加此项
self.setNeedLayout()
self.layoutIfNeeded()
3.在你的UIViewController viewDidLoad()中方法中
self.collectionView.registerNib(UINib(nibName:"yourNibName",bundle:nil),forCellWithReuseIdentifier:"Cell")
--------更新20150923
只需要第2步,重写didMoveToSuperView方法即可添加
self.setNeedLayout()
self.layoutIfNeeded()
在您的自定义UICollectionViewCell类中.
谢谢@macayer
我非常肯定这是 iOS9 的一个错误。我没有在 Storyboard 中设置UICollectionViewCellUI 和约束UICollectionView,而是创建了它自己的 XIB。然后添加到UIViewController:
self.collectionView.registerNib(UINib(nibName: "MatchmakersCollectionViewCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "CollectionCell")
Run Code Online (Sandbox Code Playgroud)
现在效果很好。
| 归档时间: |
|
| 查看次数: |
5968 次 |
| 最近记录: |