且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何判断我是否使用同一项目来填充多个UICollectionViewCell?

更新时间:2022-12-07 19:41:34

我认为这不是问题多次引用同一个项目。当更新数组中的单个数据时,似乎数组中的整个数据没有相应地更新。也许我在您的代码中找不到逻辑。

I don't think this is an issue with multiple referencing to the same item. It seems like your entire data in the array are not updated accordingly when a single data in the array is updated. Maybe I couldn't find the logic in your codes. Disregard if so.

让我们说您将两首曲目添加到一个播放列表中,然后播放了其中一首。然后,当您播放另一首曲目时,在打开第二首曲目时,第一个的isPaying值应更新为 false。您应该确定是否正在发生。

Let say you have two tracks added to a playlist and one is played. Then when you play the the other track, the fist one's isPaying value should be updated to 'false' while the second one is turned on. You should make sure if that's happening.

少量建议。


  1. 全部删除用于显示单元格数据的逻辑旁边的逻辑,例如play()函数中包含的大多数代码。将数据更新移动到可以访问整个数据的位置。 (可能是您的情况的查看控制器)然后不仅更新该单个数据的状态,还更新整个数据集,以便您将所有数据同步起来。


  1. 从Data类中删除 isPlaying并让其他管理器或控制器跟踪当前播放的歌曲,因此您不必担心总是播放一首或一首歌曲。

  1. Remove 'isPlaying' from Data class and let other manager or controller tracks the currently playing song so you don't need to worry about one or no song always playing.

尝试将有意义的数据传递给每个视图控制器。例如,您的最后一个视图控制器似乎不需要了解其他播放列表。因此,与其从playlistArray [playlistIndex] [indexPath.item]中获取歌曲,不如让它具有像playlist [indexPath.item]这样的数据结构会更好。

Trying to pass the meaningful data to each view controller. For example, it seems like your Last view controller doesn't need to know about other playlist. So rather than getting a song from playlistArray[playlistIndex][indexPath.item], it's better if you have a data structure like playlist[indexPath.item].

在对象之间进行比较时,请确保您的实现正确。听起来您可以将两首相同的歌曲添加到同一播放列表中,并且在play()函数中比较song.url似乎没有希望。甚至参考比较对我来说也很危险。你应该怎么做?在给定的背景下很难分辨。同样,如果其他一些来源通知单元格是否正在播放,并且单元格仅显示其状态,则可能会更好。

Make sure your implementation is right when you do comparison between the object. Sounds like you can add two same songs to a same playlist and comparing song.url in play() function doesn't look promising. Even reference comparison sounds dangerous to me. What you should do? Hard to tell with given context. Again, it might be better if some other source let cell know if it's currently playing or not and the cell only displays its status.

总体而言,您的要求看起来并不难。

Overall, your requirements don't look hard.