Wie kann man Sortieren von Daten in einer tableView alphabetisch nach Abschnitt mit einem custom-model-Klasse?

Ich bin neu in der coding und habe versucht, die Daten zu Sortieren (Namen) in meinem tableview Zeilen alphabetisch nach§. Ich habe es geschafft, erstellen Sie die Abschnitte, und ein index, kann aber nicht herausfinden, wie man die Namen zu Sortieren; jeder Abschnitt hat die gleiche Liste mit Namen von A-Z.

Habe ich eine model-Klasse von Namen, Objekte: nameTitle, nameDetail -, Bild -, Breitengrad-Koordinaten und Länge Koordinaten. Der tableView zeigt nameTitle.text. Die restlichen Objekte werden angezeigt oder abgerufen, die in einem anderen view-controller.

Hier ist Beispielcode aus meiner TableViewController:

import UIKit

class TableOfContentsVC: UIViewController, UITableViewDelegate,  UITableViewDataSource {

@IBAction func backButtonPressed(_ sender: Any) {

    dismiss(animated: false, completion: nil)

}

@IBOutlet weak var tableView: UITableView?

var name = [Name]()

override func viewDidLoad() {
    super.viewDidLoad()

    let cell001 = Name (nameTitle: "Acker" , nameDetail: "Details for Acker are listed here.", picture: UIImage (named: "somePicture.jpg"), latCoordinates: 38, longCoordinates: 119)

    name.append (cell001)

    let cell002 = Name (nameTitle: "Baker" , nameDetail: "Details for Baker are listed here.", picture: UIImage (named: "somePicture.jpg"), latCoordinates: 38, longCoordinates: 119)

    name.append (cell002)

    let cell003 = Name (nameTitle: "Caker" , nameDetail: "Details for Caker are listed here.", picture: UIImage (named: "somePicture.jpg"), latCoordinates: 38, longCoordinates: 119)

    name.append (cell003)

    let cell004 = Name (nameTitle: "Dacker" , nameDetail: "Details for Dacker are listed here.", picture: UIImage (named: "somePicture.jpg"), latCoordinates: 38, longCoordinates: 119)

    name.append (cell004)

    let cell005 = Name (nameTitle: "Ecker" , nameDetail: "Details for Ecker are listed here.", picture: UIImage (named: "somePicture.jpg"), latCoordinates: 38, longCoordinates: 119)

    name.append (cell005)

    let cell006 = Name (nameTitle: "Facker" , nameDetail: "Details for Facker are listed here.", picture: UIImage (named: "somePicture.jpg"), latCoordinates: 38, longCoordinates: 119)

    name.append (cell006)
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
    let currentCollation = UILocalizedIndexedCollation.current() as UILocalizedIndexedCollation
    return currentCollation.section(forSectionIndexTitle: index)
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let currentCollation = UILocalizedIndexedCollation.current() as UILocalizedIndexedCollation
    let sectionTitles = currentCollation.sectionTitles as NSArray
    return sectionTitles.object(at: section) as? String

}

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    let currentCollation = UILocalizedIndexedCollation.current() as UILocalizedIndexedCollation
    return (currentCollation.sectionIndexTitles as NSArray) as? [String]


}

func numberOfSections(in tableView: UITableView) -> Int {
    let currentCollation = UILocalizedIndexedCollation.current() as UILocalizedIndexedCollation
    let sectionTitles = currentCollation.sectionTitles as NSArray
    return sectionTitles.count

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCell(withIdentifier: "NameCell", for: indexPath) as? NameCell {

        let name = Name [indexPath.row]

        cell.updateUI(name: name)

    return cell


    } else {

        return UITableViewCell()

    }
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return name.count


}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let name = names [indexPath.row]
    performSegue(withIdentifier: "ContentVC", sender: name)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination = segue.destination as? ContentVC {

        if let name = sender as? Name {
            destination.name = name
        }
    }
}  
}

Alles funktioniert Super, außer immer die Namen zu zeigen, bis in der jeweiligen Rubrik alphabetisch geordnet. Ich habe versucht, über 10 verschiedene Möglichkeiten, diese zu arbeiten und nichts hat geholfen. Irgendwelche Vorschläge würde sehr geschätzt werden. Vielen Dank im Voraus!

InformationsquelleAutor dalex | 2017-01-14
Schreibe einen Kommentar