简体   繁体   English

集合视图单元格背景颜色在 reloadData() Swift/Firebase 上反转

[英]Collection view cell background colours reverse on reloadData() Swift/Firebase

NOTE - This question was edited significantly on 22/08/2020.注意 -这个问题在 2020 年 8 月 22 日进行了重大编辑。 Any answers or comments before this edit may not (or may still) make sense.此编辑之前的任何答案或评论可能没有(或可能仍然)有意义。 As the question (and possibly the existing answers) may be valid, I don't believe deleting the question is of benefit to others.由于问题(可能还有现有的答案)可能是有效的,我认为删除问题对其他人没有好处。

======================================= ========================================

SWIFT 5 SWIFT 5


The Problem问题

I have a collection view, which is filled with data from Firebase.我有一个集合视图,其中填充了来自 Firebase 的数据。 As part of this data, a "colour" is passed for each item.作为该数据的一部分,为每个项目传递一个“颜色”。 This colour is an int, which the cell looks up in a table and draws.这种颜色是一个 int,单元格在表格中查找并绘制它。

在此处输入图像描述

A fragment of the code used to assign the colour:用于分配颜色的代码片段:

let purpleGrad = [#colorLiteral(red: 0.4392156863, green: 0.4193316882, blue: 1, alpha: 1),#colorLiteral(red: 0.4392156863, green: 0.4193316882, blue: 1, alpha: 1)]
let blueGrad = [#colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1),#colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)]
let pinkGrad = [#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1),#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)]
let yellowGrad = [#colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1),#colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)]
        
let colorsTable2: [Int: [UIColor]] = [0: purpleGrad, 1: blueGrad, 2: pinkGrad, 3: yellowGrad, 4: greenBlue]

When the collection view first draws, the colours show in the correct order.当集合视图第一次绘制时,颜色以正确的顺序显示。 When the prices update from Apple (when reloadData() is called), the collection view then draws backwards.当 Apple 更新价格时(调用reloadData()时),集合视图会向后绘制。

In the following picture, the left side is how the collection view loads before the prices (which is correct, and always loads with the correct colours).在下图中,左侧是集合视图在价格之前加载的方式(这是正确的,并且始终以正确的颜色加载)。 The right side is after the prices have loaded (which is a reversal of the left side, and always loads this exact way).右侧是在价格加载之后(这是左侧的反转,并且总是以这种方式加载)。

在此处输入图像描述


Possible Solutions可能的解决方案

Previous comments suggested dictionaries may be the cause of this error - whilst this may still be the case, I do not believe this is the issue.以前的评论建议字典可能是导致此错误的原因 - 虽然情况可能仍然如此,但我不认为这是问题所在。 The ordering is always correct, and the colours ALWAYS reverse exactly, rather than just redrawing with random colours.顺序总是正确的,颜色总是完全相反,而不是仅仅用随机颜色重新绘制。

I am unsure as to what else to try - to my knowledge, reloadData() is required to update the collection view when the price is pulled.我不确定还有什么可以尝试的——据我所知,当价格被拉低时,需要 reloadData() 来更新集合视图。

Sometimes when tableview cells get messy by refreshing or scrolling, it is because there is no default value for data which is changing in each cell.有时,当 tableview 单元格因刷新或滚动而变得混乱时,这是因为每个单元格中正在更改的数据没有默认值。 So you might want to consider a primary value for color Int (like 0) and set cell's background color to nil, before fetching and setting actual values.因此,在获取和设置实际值之前,您可能需要考虑颜色 Int 的主值(如 0)并将单元格的背景颜色设置为 nil。

This is a wild guess because there's too much code but you're using a lot of NSDictionaries (please use Swift Dictionary).这是一个疯狂的猜测,因为代码太多但您使用了很多 NSDictionaries(请使用 Swift 字典)。

Dictionaries are unordered pairs so you cannot rely on the order of the data when iterating over it.字典是无序对,因此在迭代数据时不能依赖数据的顺序。 So for example here:所以例如这里:

let course = course as? NSDictionary
for (courseID, courseData) in course! {
   let courseData = courseData as? NSDictionary ?? NSDictionary()
   let subjects = courseData["subjects"] ?? NSDictionary()

That's going to iterate over the data in course potentially different each time it's run (the child data is unordered).这将在每次运行时迭代数据当然可能不同(子数据是无序的)。

So you probably have some disconnect between the ordering of the data and getting the associated background color.因此,您可能在数据排序和获取相关背景颜色之间存在一些脱节。

EDIT编辑

The data returned from Firebase is being cast to a Dictionary by your code, that will then loose the ordering.从 Firebase 返回的数据正在被您的代码转换为字典,然后将失去排序。

let downloadedCourses = snapshot.value as? NSDictionary

If you want to maintain the ordering and use the child data as snapshots (recommended) do this如果您想保持顺序并将子数据用作快照(推荐),请执行此操作

let orderedCourses = snapshot.children.allObjects as! [DataSnapshot]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 集合视图的reloadData方法显示选定的单元格 - Collection View reloadData method shows a cell as selected 如何在Swift iOS中更改自定义“收藏夹视图”单元格的背景颜色? - How to change the background color of a custom Collection View cell in swift iOS? Swift Animate Background无法从集合视图单元中工作 - Swift Animate Background not working from a collection view cell 在cellForItemAt集合视图中不通过Swift 3中的collectionView.reloadData()更新indexPath - In cellForItemAt collection view is not updating indexPath by collectionView.reloadData() in swift 3 Swift –编辑单元格内容后,Table View reloadData无法正常工作 - Swift – Table View reloadData not working after editing contents of cell SWIFT-集合视图单元格大小 - SWIFT - Collection View Cell size 在集合单元格中添加背景视图 - Add background view in collection cell 快速自定义集合视图单元格 - Custom Collection view cell in swift 背景颜色在多个细胞选择Swift中自动变化 - Background colours changing automatically in Multiple Cell Selection Swift Swift - Firebase - 异步下载图像和数据会导致集合视图单元格中的显示错误 - Swift - Firebase - Downloading images and data asynchronously leads to wrong display within the collection view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM