Ich habe eine einfache App zeigt Funktionäre, Funktionen, Mitarbeiter, etc ...Indexed Tableview (A bis Z) nicht richtig funktioniert
ich ein SearchBar eingefügt und es funktioniert super! Aber ich habe etwas schwer, eine indexierte TableView zu setzen (ich entschied mich, UILocalizedIndexedCollation
zu verwenden). Werfen Sie einen Blick auf meine Haupttableviewcontroller:
import UIKit
class Page1: UITableViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
var employeesSearching = [Employee]()
var isSearching : Bool = false
let collation = UILocalizedIndexedCollation.current()
var sections: [[AnyObject]] = []
var objects: [AnyObject] = [] {
didSet {
let selector: Selector = #selector(getter: UIApplicationShortcutItem.localizedTitle)
sections = Array(repeating: [], count: collation.sectionTitles.count)
let sortedObjects = collation.sortedArray(from: objects, collationStringSelector: selector)
for object in sortedObjects {
let sectionNumber = collation.section(for: object, collationStringSelector: selector)
sections[sectionNumber].append(object as AnyObject)
}
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.delegate = self
self.tableView.contentOffset = CGPoint(x: 0, y: searchBar.frame.height)
}
override func numberOfSections(in tableView: UITableView) -> Int { return sections.count }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return isSearching ? employeesSearching.count : sections[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
let entry = isSearching ? employeesSearching[indexPath.row] : Shared.instance.employees[indexPath.row]
//Do I have to put (sections[indexPath.section][indexPath.row]) here? ok, but how?
cell.nameLabel.text = entry.name
cell.positionLabel.text = entry.position
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return collation.sectionTitles[section]
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return collation.sectionIndexTitles
}
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return collation.section(forSectionIndexTitle: index)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if self.searchBar.text!.isEmpty {
self.isSearching = false
self.tableView.reloadData()
} else {
self.isSearching = true
self.employeesSearching.removeAll(keepingCapacity: false)
let searchText = self.searchBar.text!.lowercased()
for employee in Shared.instance.employees {
if employee.name.lowercased().range(of: searchText) != nil {
self.employeesSearching.append(employee)
}
}
}
self.tableView.reloadData()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowP2" {
if let indexPath = tableView.indexPathForSelectedRow {
let employee: Employee
isSearching == true && searchBar.text != "" ?
(employee = employeesSearching[indexPath.row]) : (employee = Shared.instance.employees[indexPath.row])
let destination = segue.destination as? Page2
destination?.newPage = employee
}
}
}
}
EDIT 1: Meine Daten
ich meine Daten von einem plist vom AppDelegate importieren:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String:Any]] {
Shared.instance.employees = array.map{Employee(dictionary: $0)}
}
return true
}
Es war alles ok (und nicht leer), bevor ich UILocalizedIndexedCollation
verwendet habe, aber jetzt ist alles leer:
Was ist das Problem, vor dem Sie stehen? –
TableView ist leer, weil ich nicht weiß, wie Abschnitte auf func 'cellForRowAt' –