解決 CollectionView 上拉加載更多 刷新閃屏

當(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)
}
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容