ios - Populating a UITableView with multiple sections correctly? -


i new programming ios , i'm running weird problem can't figure out. trying populate table follows. there initial screen user selects categories - sports leagues. populate array of strings names of leagues, become headers sections of table. league names correspond other arrays, have each team in league, number of teams in each league differ. these arrays should populate correct section (i.e. number of rows per section not static depends on leagues user selects).

i have in numberofrowsinsection function:

func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {    if leagues[section] == "nba" {        if (!contains(allteams, nbateams[0])) {             allteams += nbateams        }    }    return nbateams.count . . . } 

i have multiple clauses each of teams. attempting use "allteams" array in cellforindexpath function 2 things happening don't want happen. firstly, allteams array not populated in same order leagues array in, found out through print statements happens in numberofrowsinsection - reason, section parameters starts @ greatest section number , goes 0 , starts counting up...weird. secondly, sections populated in repetitive manner. is, first section , second section populated same teams (or how many sections user wanted), though number of teams populates correct (so return statements in numberofrowsinsection correct).

i feel i'm missing these functions come tableviewdatasource protocol. i'm sorry if mouthful, thought easier read through entire class. can provide! also, if there's altogether better approach this, please let me know!

i figured out! in cellforrowatindexpath, rather using allteams populate sections, created constant correct league , used couple of if statements since don't have ton of leagues:

let currentleague = leagues[indexpath.section] if currentleague == "nba" {     cell.textlabel?.text = nbateams[indexpath.row] } 

etc.


Comments