Reorder UicollectioView items using RealmSwift on drag and drop

徘徊边缘 提交于 2019-12-04 21:15:47
TiM

I answered a similar question recently, but I'll re-explain it here. :)

Easily, the best and quickest way to re-order Realm objects inside a Realm file is to make an overarching List object that holds all of the Realm objects of a given type.

For example in this case, you make another object to hold that allStories value you already created:

// Model class that manages the ordering of story album objects
class StoryAlbumDMList: Object {
   let allStories = List<StoryAlbumDM>()
}

// Model class for the actual story album objects
class StoryAlbumDM: Object {
   dynamic var id = 0
   dynamic var type = ""
   dynamic var isImage: Int = 0
   dynamic var textData = ""
   dynamic var imageData: NSData? = nil
   dynamic var rowId: Int = 0
   dynamic var position: Int = 0
   dynamic var storyId: Int = 0
   dynamic var isCoverImage: Int = 0
   dynamic var imagePath = ""
}

This way, when you want to re-order the list, all you need to do is re-order them inside this array.

Like I said in the other question, one other way you can do it (Which is not as good, but also doesn't require an extra Realm object) is to add another property named orderedIndex, which simply contains a number indicating the numerical order of these objects. When you want to re-order them, it's simply a matter of re-setting these numbers.

Let me know if you need any more clarification!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!