I have a simple app showing functionaries, functions, employees, etc…
I inserted a SearchBar and it is working great! But I am having some difficult to put a indexed TableView (I decided to use UILocalizedIndexedCollation
). Take a look at my main TableViewController:
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: My data
I am importing my data from a plist by the AppDelegate:
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
}
It was all ok (and not empty) before I used UILocalizedIndexedCollation
, but now it is all empty:
答案 0 :(得分:0)
You have implement TableView cellforRowAt, Problem could be
1) Your labels are not constraint well. You can check it by adding a background color of the labels.
2) You don't have any data.