简体   繁体   English

如何查询我的数据并将其按类别存储在数组中,以便在TableView中按类别显示它们?

[英]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]] ,其中keyevent_categoryvalue是位于该event_categoryarray of Events

Now, since you need an array for UITableViewDataSource , you need to create an array from groupedEventsDict . 现在,由于需要UITableViewDataSourcearray ,因此需要从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.

相关问题 如何从类别表重新加载RootViewController中列出的tableView? - How can I reload the tableView that listed in RootViewController from a category table? 我正在尝试让图标数组显示在我的天气应用中,但似乎无法获取UIImage来显示它们 - I am trying to get an icon array to display in my weather app, but can not seem to get UIImage to display them 如何在tableView中显示此信息? - How can I display this information in my tableView? 在 Firestore 中使用.getDocuments 并尝试将文档 ID 放入数组并使用 tableview 显示它们后无法显示数据 - Cant display data after using .getDocuments in Firestore and trying to place the document IDs into an array and displaying them using a tableview 如何在iOS中存储像类别这样的数据 - How to store the data like category in ios 如何将Firebase数据存储在数组中并将其打印出来? - How to store Firebase data in array and print them out? 警报“登录iTunes Store”和“电池电量不足”会暂停我的应用程序卡纸。我怎样才能取消它们? - Alert “Sign in to iTunes Store” and “Battery low” pauses my apps jams. How can I unpause them? 如何在TableView中显示Firebase数据? - How can I display Firebase data in TableView? 如何从UIViewcontroller类调用类别方法? - How can I call a category method from my UIViewcontroller class? 如何获取数据以保存到表视图中的特定类别,而不是所有类别? - how to get data to save to specific category in tableview instead on all categories?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM