Ho un elenco di nomi in ordine alfabetico, e ora voglio visualizzare questi nomi in una vista tabella. Sto lottando con il raggruppamento di questi nomi per ogni lettera.Sezioni alfabetiche nella vista tabella tabella in swift
Il mio codice è simile al seguente:
let sections:Array<AnyObject> = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var usernames = [String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellID = "cell"
let cell: UITableViewCell = self.tv.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
cell.textLabel?.text = usernames[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return usernames.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 26
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
return self.sections
}
func tableView(tableView: UITableView,
sectionForSectionIndexTitle title: String,
atIndex index: Int) -> Int{
return index
}
func tableView(tableView: UITableView,
titleForHeaderInSection section: Int) -> String?{
return self.sections[section] as? String
}
e tutto funziona abbastanza bene, tranne per il raggruppamento che rende la mia vista tabella finire così:
Quindi io ti conosco dovrebbe essere in grado di utilizzare la funzione filtrata in una matrice, ma non ho capito come implementarla.
Qualsiasi suggerimento su come procedere sarebbe apprezzato.
'cell.textLabel? .text = username [indexPath.row]' è la vostra fonte di problema. Continui a compilare ogni sezione con gli stessi identici dati. –
Sì, ho solo bisogno di filtrare correttamente l'array di nomi utente :) – martin
mentre ci sono alcuni tentativi sotto - Ho trovato questo obiettivo-c più solido https://github.com/chrisladd/CGLAlphabetizer/blob/master/Example/CGLAlphabetizerDemo/ CGLContactsTableViewController.m – johndpope