【发布时间】:2015-12-14 08:46:18
【问题描述】:
自从我迁移到新的 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
}
而且每次我更新数据源(self.matchmakers)时,我都会打电话给self.collectionView.reloadData()
我注意到的最后一件事很奇怪,当使用 Xcode 调试调试视图层次结构时,UICollectionViewCell 从未正确显示子视图,只是给了我一个默认的 UIView 代替它们:
【问题讨论】:
标签: ios uicollectionview uicollectionviewcell ios9 xcode7