當(dāng) collectionView 在加載更多時(shí),網(wǎng)絡(luò)數(shù)據(jù)返回后,直接添加到模型數(shù)組里面,然后調(diào)用 collectionView.reloadData
,這樣每次刷新,屏幕都會(huì)閃一下,多么刺眼。
沒(méi)有上圖的原因是,錄屏生成的 Gif 圖片沒(méi)法看清閃屏現(xiàn)象。
解決辦法
用
collectionView?.insertItemsAtIndexPaths:_
來(lái)代替collectionView.reloadData
-
示例代碼:
// 模型數(shù)組
var posts = Post {
didSet {
let addCount = posts.count - oldValue.count
// 這里可以判斷 addCount 是否大于 0,等于 0 直接 return
var indexPathArray = NSIndexPath
for count in 0..<addCount {
let indexPath = NSIndexPath(forItem: oldValue.count + count, inSection: 0)
indexPathArray.append(indexPath)
}
collectionView?.insertItemsAtIndexPaths(indexPathArray)
}
}