updateSearchResultsForSearchController从哪里获取数据?

时间:2015-04-05 12:17:23

标签: ios swift

我正在尝试了解updateSearchResultsForSearchController的工作原理。我目前正在关注一本书中没有很好解释的例子。

我有一个由ViewController.swift管理的数据表和另一个文件SearchResultsController.swift来管理该表中的搜索结果。

数据来自 ViewController.swift

中的字典
    class ViewController: UIViewController, UITableViewDataSource {
        var searchController: UISearchController!
        var names: [String: [String]]!

        override func viewDidLoad() {
            names = ["A": ["Abe", "Anna"], "B":["Bob"]]     

            let resultsController = SearchResultsController()
            resultsController.names = names
            searchController = UISearchController(searchResultsController: resultsController)

            let searchBar = searchController.searchBar
            searchBar.sizeToFit()
            tableView.tableHeaderView = searchBar
            searchController.searchResultsUpdater = resultsController
        }
    }

SearchResultsController.swift 的实现类似于:

class SearchResultsController: UITableViewController, UISearchResultsUpdating {
    var names:[String:[String]] = [String:[String]]()
    var filteredNames: [String] = []

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        let searchString = searchController.searchBar.text
        filteredNames.removeAll(keepCapacity: true)

        if (!searchString.isEmpty) {
            let filter: String -> Bool = { name in

            let nameLength = countElements(randomVar)
            println("nameLength for name \(randomVar) is \(nameLength)")
        }
    }
}

检查日志,我看到了:

nameLength for name Abe is 3
nameLength for name Anna is 4
nameLength for name Bob is 3
  1. SearchResultsController如何才能看到字典名称中的数据?
  2. 它似乎在迭代数组名称。如何在 SearchResultsController.swift 中的这行代码中将数组的每个元素分配给变量 name

    let filter:String - > Bool = {name in

0 个答案:

没有答案