根据后端的数据为UITableView中的节设置数据源 [英] Setting up data source for Sections in UITableView Based off of Data from Backend

查看:79
本文介绍了根据后端的数据为UITableView中的节设置数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何利用表格视图&表格视图单元格,今天我想练习使用表格视图中的部分.设置节标题并将数据分类到这些特定节中非常容易,尤其是在使用枚举时.

I'm learning how to utilize table views & table view cells and today I wanted to practice using sections within table views. Setting the section title and sorting the data into those specific sections is fairly easy, especially when using enums.

为了使其更具挑战性,我想看看将基于团队名称的部分标题存储在Firebase上并使行由属于该特定团队的用户组成将是多么困难.我已经意识到,对于仍在学习Swift的人来说,这是极其困难的.

To make it a bit more of a challenge, I wanted to see how difficult it would be to have the section titles based off of Team Names stored on Firebase and have the rows consist of users that are apart of that specific team. I've come to realize that this is extremely difficult for someone that's still learning Swift.

此处显示了该结构在Firebase上的外观:

Heres how the structure looks on firebase:

用户可以创建自己的团队名称&这些队名将作为头衔.因此,我要做的是创建一个字符串数组,观察团队名称并将其添加到该数组中.从那里,我将各部分的标题设置为每个团队的名称. 我遇到的问题是将正确的用户归入每个团队.

The users have the ability to create their own team names & these team names will serve as the titles. So what I've done is created an array of strings, observed the team names and added them to the array. From there, I set the titles of the sections to each team name. The problem Im having is putting the correct users under each team.

如果有40个团队,我可以轻松地创建40个标题正确的部分,但似乎无法弄清楚如何将每个用户分类为正确的部分.

If there were 40 teams, I could easily create 40 sections with the correct titles, but I can't seem to figure out how to sort each user into the correct section.

推荐答案

这是您问题的简单实现.您有一个播放器类,它是您的Firebase数据.

This a simple implementation of your problem. You have a player class which is your Firebase data.

class Player {
    var name: String
    var teamName: String

    init(name: String, teamName: String) {
        self.name = name
        self.teamName = teamName
    }
}

创建一个Team类来存储您的节数据.

Create a Team class to store your section data.

class Team {
    var teamName: String
    var players: [Player]

    init(teamName: String, players: [Player]) {
        self.teamName = teamName
        self.players = players
    }
}

从Firebase获取数据后,将所有播放器存储在数组players中.现在,您可以使用一个方法,该方法返回一个包含Team对象的数组,该对象具有teamNameplayers,这是一个包含共享相同teamName

Store all the players in an array players after you get the data from Firebase. Now you can have a method which returns an array which contains Team objects which has teamName and players which is an array containing the filtered players of the sharing the same teamName

func getTeams() -> [Team] {
    var teams: [Team] = []
    while !players.isEmpty {
        guard let referencePlayer = players.first else {
            print("All players sorted.")
            return []
        }
        let filteredPlayers = players.filter { (player) -> Bool in
            return player.teamName == referencePlayer.teamName
        }
        players.removeAll { (player) -> Bool in
            return player.teamName == referencePlayer.teamName
        }
        let team = Team(teamName: referencePlayer.teamName, players: filteredPlayers)
        teams.append(team)
    }
    return teams
}

现在在viewDidLoad中调用此方法并重新加载表.

Now call this method in viewDidLoad and reload the table.

func numberOfSections(in tableView: UITableView) -> Int {
    return teams.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return teams[section].players.count
}

这篇关于根据后端的数据为UITableView中的节设置数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆