
显然,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:
>使用Nib(.xib)文件创建自定义UICollectionVIEwCell.
>在自定义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
总结以上是内存溢出为你收集整理的UICollectionViewCell布局问题iOS9全部内容,希望文章能够帮你解决UICollectionViewCell布局问题iOS9所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)