2017-04-10 67 views
0

我有一個顯示工作人員,職能,員工等一個簡單的應用程序...索引的TableView(A至Z)工作不正常

我插入了搜索欄,它是偉大的工作!但我有一些困難的索引TableView(我決定使用UILocalizedIndexedCollation)。在我的主要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 
      } 
     } 
    } 
} 

編輯1:我的數據

我從一個plist中由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 
} 

這是在我使用UILocalizedIndexedCollation之前一切正常(並且不是空的),但現在它全部爲空:

app

+1

您面臨的問題是什麼? –

+0

TableView是空的,因爲我不知道如何在func上使用部分'cellForRowAt' –

回答

0

你必須執行的TableView cellforRowAt,問題可能是

1)您的標籤沒有約束好。您可以通過添加標籤的背景顏色來檢查它。

2)您沒有任何數據。

+0

剛剛更新了我的問題。我的約束是好的,也是我的數據。我知道問題出在'cellForRowAt'上。我只是不知道如何解決它。 –