【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

 

   

posted @ 2019-12-02 23:48  Mr·Xu  阅读(2271)  评论(1编辑  收藏  举报