【iOS bug記錄】UICollectionviewCell刷新變得這么莫名其妙?


 

  項目是一個即時聊天的社交軟件,聊天流采用的是UICollectionView,隨着進度的完善,發現一個特別的bug,UICollectionviewCell的復用,並沒有直接insert進去,而是出現了莫名奇妙的插入方式,

  這不是我的圖,這是我在網上找到的,跟我的效果一樣一樣的。link the image 

  起初我以為是動畫出了問題,導致keyboard跟UICollectionView起了沖突,可后來發現,關閉了所有的動畫也不起作用。折騰了一天,終於在stackOverflow上找到了答案。

  https://stackoverflow.com/questions/56584816/swift-uicollectionview-cell-displayed-scrolled-in-to-the-view-incorrectly-when-k

  這位大佬的情況跟我的一樣一樣的,可他沒有給出具體的解決辦法,只是提出了一些寶貴建議。

  After long investigation I noticed that the same phenomen happens also at the bottom in particular cases. I am not sure it it is bug or custom layout is necessary, but with the simple flow layout I solved the issue by setting collectionView contraints beyound the display edge.

  This force to call displaying cell earlier and gives some time to lay cell approprietely even hide keyboard animation is used. Of coarse collectionView top and bottom contentInset has to be set in viewDidLoad and handle any contentInset change during runtime accordingly

  Hope it help!

  抽重點:設置collectionView限制超出顯示邊緣。這迫使調用顯示單元格更早,並給一些時間,以奠定適當的單元格,甚至隱藏鍵盤動畫使用。頂部和底部的contentInset必須在viewDidLoad中設置,並在運行時相應地處理任何contentInset的變化

  (我做的是,將底部約束加+1,超出父類的bottom,這樣解決了collectionview莫名其妙的插入,但是這樣卻寫死了collectionview的約束,無法使列表跟隨鍵盤改變contentInset。如果約束<1,又會出現奇怪問題,我也不知這是為何。)

  經過一番折騰之后,我發現通過主線程延時更改collectionview的contentInset也能有效解決列表的刷新問題

  補充:1、如何解決UICollectionView進行reload時,閃爍問題?

  傻逼方法:reload之前先設置alpha = 0,結束后alpha = 1 

//            self.messagesCollectionView.reloadData()
//            self.messagesCollectionView.performBatchUpdates({
//            }) { (finish) in
//                self.messagesCollectionView.alpha = 1
//            }

  補充 :2、解決UITableView或UICollectionView中cell影響UIButton的高亮效果

  解決辦法:

tableView.delaysContentTouches = false

 

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM