Ios 在UICollectionView中,订单会自动更改
我有个大问题 我在UICollectionView中滚动。 当绘图区域滚动过多时,排列顺序变得不连贯 即使滚动,我也不想更改顺序。 我该怎么办 帮帮我Ios 在UICollectionView中,订单会自动更改,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有个大问题 我在UICollectionView中滚动。 当绘图区域滚动过多时,排列顺序变得不连贯 即使滚动,我也不想更改顺序。 我该怎么办 帮帮我 let titles = ["1","2","3","4","5","6","7","8", "9", "10", "11", "12"] //titles func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func c
let titles = ["1","2","3","4","5","6","7","8", "9", "10", "11", "12"] //titles
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = testCollectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath) as! TestCell //customCell
let testTitle = cell.contentView.viewWithTag(2) as! UILabel
testTitle.text = titles[indexPath.row]
return cell // yeah, all done. is these code is normal. right? But when i scroll this UICollection, change the order in Outside drawing area.
}
您的代码不应导致单元格更改顺序。我想知道您在使用
viewWithTag
时是否有问题。在集合视图/表视图单元格中查找视图是一种相当脆弱的方法
您已经有了自定义单元格类型。我建议在自定义单元格类型中为标签创建一个出口,并以这种方式引用标签:
class TestCell: UICollectionViewCell {
//(Connect this outlet in your cell prototype in your Storyboard
@IBOutlet titleLabel: UILabel!
}
//And in your view controller's cellForItemAt method...
cell.titleLabel.text = titles[indexPath.row]
您绝对不应该使用带标签的
视图
。这样做的原因是因为单元格已退出队列,这意味着通过滚动从屏幕上取下的单元格将被重新用于即将出现在屏幕上的单元格。这样做是为了节省内存。有时,订单的问题可能是因为以前使用的单元格在呈现给用户时更新不够快。如果您使用的是网络请求,解决方案可以是首先简化请求,然后缓存返回的任何繁重数据,如图像。不,其顺序从未更改。在我以前的经历中,这件事从来没有发生过。嗯,我应该重写我的代码:u(如果你有时间,那么等待别人的评论。也许这是为别人发生的!