我有一个IDs ["one", "two", "three"...]数组
这些Ids与我从URLSession获得的json相匹配,然后显示在collectionview中。目前,它只是一个简单的collectionView与功能,以重新排序,通过长按压和拖动。但我需要把它分成几部分。
最好的方法是使节具有动态性,这样我就可以添加任意多个节,然后将单元格排序到各个节中。我得到了我能做的,如果我有一个数组,为每一节,如
section1 = ["one", "two"]
section2 = ["three", "four"]
但我不知道如何在我目前的设置中意识到这一点。
发布于 2017-09-05 09:28:46
不要这样做。我相信你可以想出一种方法来保持单一数组,但不要。维护噩梦会困扰你。
创建数据视图,该视图被分解为数组。
struct MySection {
let sectionId: String // or whatever section metadata you need.
let data: [Data] // or whatever your real type is.
}在集合视图控制器中。
var dataView: [MySection]一节中的所有数据
self.dataView = [MySection(sectionId: "all data", data: self.data)]将数据分组到不同的部分
self.dataView = self.data.reduce([]) { dataView, datum in
var dataView = dataView // make a mutable data view
let sectionId = self.determineSectionIdFromDatum(datum) // <-- slotting into sections
// Add into an existing section or into a new section
if let index = dataView.index(where: { $0.sectionId == sectionId }) {
let data = dataView[index].data + [datum]
dataView[index] = MySection(sectionId: sectionId, data: data)
} else {
let data = [datum]
dataView.append(MySection(sectionId: sectionId, data: data))
}
return dataView
}此时,您可能需要对节或节中的数据进行排序。
https://stackoverflow.com/questions/46050909
复制相似问题