[英]Custom CollectionView cells' layout elements disappear when attempting to update the cell
I have a custom cell for a collection view, which is set in the storyboard and has outlets connected to a corresponding cell class. 我有一个用于集合视图的自定义单元格,该单元格设置在情节提要中,并具有连接到相应单元格类的插座。
class MyCustomCollectionViewCell: UICollectionViewCell {
// MARK: Outlets
@IBOutlet weak var customImage: UIImageView!
@IBOutlet weak var customProgress: UIProgressView!
@IBOutlet weak var customLabel: UILabel!
}
The initial setup works perfectly, I am setting a custom image, name and progress according to a particular state of an item in my database. 初始设置可以完美运行,我正在根据数据库中某项的特定状态设置自定义图像,名称和进度。
I want to update the collection view to reflect changes, especially regarding the status bar. 我想更新集合视图以反映更改,尤其是有关状态栏的更改。 Also, once the status bar is finished, the image should change also.
同样,一旦状态栏结束,图像也应更改。
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(MyViewController.updateUI), userInfo: self, repeats: true)
Using a timer started when the view appears, I want to update the view every couple of seconds (I chose 3 to effectively test it, but it could also be as long as maybe 20 seconds). 我使用一个在视图出现时启动的计时器,希望每隔几秒钟更新一次视图(我选择3来进行有效测试,但它可能也要长达20秒)。
The problem is, I don't know what method to use here, as the all have specific drawbacks: 问题是,我不知道在这里使用哪种方法,因为所有方法都有特定的缺点:
reloadSections(mySection)
and reloadData()
, cause all cells to slightly flicker (which would be ok) but cause all progress bars to completely disappear after the first update. reloadSections(mySection)
和reloadData()
会导致所有单元格略微闪烁(可以),但会导致所有进度条在第一次更新后完全消失。 setNeedsLayout()
and setNeedsDisplay()
seem to have no effect at all. setNeedsLayout()
和setNeedsDisplay()
似乎完全无效。 Nothing (visible) happens. All methods were called in on self.collectionView
like so: 所有方法都在
self.collectionView
上调用, self.collectionView
所示:
@objc private func updateUI() {
// self.collectionView?.reloadSections(singleSection)
// self.collectionView?.reloadData()
// self.collectionView?.setNeedsLayout()
// self.collectionView?.setNeedsDisplay()
}
(I tried them all one by one) (我一一尝试了)
How can I update my view (automatically) without the progress bars disappearing? 如何在进度条不消失的情况下自动更新视图?
I think that you can use dispatch_async, that runs in another thread. 我认为您可以使用在另一个线程中运行的dispatch_async。
Put a flag in collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
, if true change only the image. 如果设置为true,则仅将图像更改为
collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
的标志。
In the cell, start a loop on progress with 在单元格中,通过
dispatch_async(dispatch_get_main_queue(), {
//do staff to controle your progress state
// set your flag to true if progress is ok
// if ok then reload the collection
CollectionView.reloadData()}
Now the flag is true, update your image. 现在该标志为true,更新您的图像。 Haven't tried it, but I think that it could be a possible way.
还没有尝试过,但是我认为这可能是一种可能的方式。
After I implemented an automatic update for the cells in a separate queue (as user2718075 suggested), I still had the problem of disappearing views. 在对单独队列中的单元实施自动更新后(如user2718075建议),我仍然遇到视图消失的问题。 This would happen every time the view(s) with the progress bars would disappear from sight when I scrolled and looked at other cells where I hid the progress bar through
customProgress.hidden = true
, because they were done. 每当我滚动并查看通过
customProgress.hidden = true
隐藏进度条的其他单元格时,带有进度条的视图就会消失,因为它们已经完成了。
This made me realize that this issue had to do with cell reuse. 这使我意识到这个问题与单元重用有关。 The cells with the progress bars would be used for another cell without a progress bar, thus it was hidden.
具有进度条的单元格将用于没有进度条的另一个单元格,因此将其隐藏。 After dequeuing and reusing it again for a cell that needed a progress bar, I did not call
customProgress.hidden = false
. 在需要进度条的单元中出队并再次使用它之后,我没有调用
customProgress.hidden = false
。 After adding that line to the cell configuration, the problem was gone. 将那条线添加到单元配置后,问题就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.