ios - 索引 TableView(A 到 Z)无法正常工作

标签 ios swift uitableview indexing

我有一个简单的应用程序,显示官员、职能、员工等……

我插入了一个搜索栏,它工作得很好!但我在放置索引 TableView 时遇到了一些困难(我决定使用 UILocalizedIndexedCollat​​ion )。看看我的主 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:我的数据

我正在通过 AppDelegate 从 plist 导入数据:

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
}

在我使用UILocalizedIndexedCollat​​ion之前一切正常(而且不为空),但现在一切都是空的:

app

最佳答案

您已经实现了 TableView cellforRowAt,问题可能是

1)你的标签没有很好地约束。您可以通过添加标签的背景颜色来检查它。

2) 您没有任何数据。

关于ios - 索引 TableView(A 到 Z)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313566/

相关文章:

ios - 如何获取本地化 "Purchased"和 "Purchased on My iPhone"播放列表的 MPMediaPlaylist?

ios - 在 Xcode 7.2 中将 PDF 用于图标图像

ios - iOS 如何在关闭应用后展示广告?

ios - Core Data Objective - C 中的 __NSFrozenArrayM 是什么

ios - 如何精确检测何时触摸 SKShapeNode?

arrays - Swift:为什么创建空白数组时需要空括号?

ios - JSON 文件直到 UITableView 才加载

ios - 如何在 iOS 中设置删除图标而不是删除文本以编辑表格 View 模式?

swift - 如何根据其可见元素调整表格 View 单元格的高度?

iOS:无法设置导航栏色调颜色