Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

ios - Setting up data source for Sections in UITableView Based off of Data from Backend

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.

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.

Heres how the structure looks on firebase:

enter image description here

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
    }
}

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
    }
}

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
}

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...