[英]How can I query my data & store them by category in an array so as to display them by category in a TableView?
I wish to display my events by category in my TableView where each TableViewCell is a category, with its events found in a CollectionView embedded within the TableViewCell. 我希望在我的TableView中按类别显示事件,其中每个TableViewCell都是一个类别,其事件可以在嵌入在TableViewCell中的CollectionView中找到。
Here is a sample initialised prototype of what I want to achieve. 这是我想要实现的样本初始化原型。
var events = [Events]()
var eventCategory = [EventCategory]()
var testEvent1 = Events(id: 1, event_name: "PrototypeEvent", event_category: "Party", event_date: "10/06/19", event_img_url: "null")
var testEvent2 = Events(id: 2, event_name: "PrototypeEvent2", event_category: "Music", event_date: "11/06/19", event_img_url: "null")
override func viewDidLoad() {
super.viewDidLoad()
var eventArray = [testEvent1]
var eventArray1 = [testEvent2]
var category1 = EventCategory(title: "Party", events: eventArray)
var category2 = EventCategory(title: "Music", events: eventArray1)
eventCategory.append(category1)
eventCategory.append(category2)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventCategory.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 245
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PopularCell", for: indexPath) as! PopularCell
let category = eventCategory[indexPath.row]
for event in category.events! {
print("Event Name:\(event.event_name)")
}
cell.eventCategory = category
return cell
}
make an enum
for event_category: 为event_category
enum
:
enum EventCategory {
case music
case party
...
}
then use the grouping init of Dictionary
然后使用
Dictionary
的分组初始化
let eventsDictionary = Dictionary(grouping: events) { (element) -> EventCategory in
return element.event_category
}
you will have a dictionary like this [EventCategory: [Events]]
; 您将有一个像这样的字典
[EventCategory: [Events]]
; it's will be an easy use for tableView
indexPath.row
to show a category for every row tableView
indexPath.row
可以很容易地为每一行显示一个类别
You can group the Events based on category using Dictionary's init, ie 您可以使用Dictionary的init根据类别对事件进行分组,即
init(grouping:by:)
Creates a new dictionary whose keys are the groupings returned by the given closure and whose values are arrays of the elements that returned each key.
创建一个新词典,其键是给定闭包返回的分组,其值是返回每个键的元素的数组。
var groupedEventsDict = Dictionary(grouping: events) { $0.event_category }
groupedEventsDict
will be of type [String:[Events]]
, where key
is the event_category
and value
is the array of Events
lying under that event_category
. groupedEventsDict
将为[String:[Events]]
,其中key
是event_category
, value
是位于该event_category
下array of Events
。
Now, since you need an array
for UITableViewDataSource
, you need to create an array
from groupedEventsDict
. 现在,由于需要
UITableViewDataSource
的array
,因此需要从groupedEventsDict
创建一个array
。
var groupedEventsArr = Dictionary(grouping: events) { $0.event_category }.compactMap({( $0.key, $0.value )})
So, your dataSource
methods look something like: 因此,您的
dataSource
方法看起来像:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groupedEventsArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PopularCell", for: indexPath)
let (category, events) = self.groupedEventsArr[indexPath.row]
print("Category: \(category)")
events.forEach {
print("Event Name:\($0.event_name)")
}
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.