searchDisplayController

searchDisplayController

我正在尝试使用TableView设置searchBar,以便可以通过表视图进行过滤,而我正在以编程方式进行所有操作。尽管我的代码不断发回错误:unexpectedly found nil while unwrapping an Optional value。错误在func numberOfRowsInSection内部,已将其标记出来。这与SearchDisplayController有关,我认为这是零。码:

struct UserMatches {
    var finalMatchesName : String
    var finalMatchesAge : Int
    var finalMatchesLocation : PFGeoPoint
    var finalMatchesImage : NSData
}

class Matches: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate {

    var tableView = UITableView()
    var searchBar = UISearchBar()

    var userMatches = [UserMatches]()
    var filterUsers = [UserMatches]()

    override func viewDidLoad() {
        super.viewDidLoad()
        createTableView()
    }

    func filterContentForSearchText(searchText: String) {
        self.filterUsers = self.userMatches.filter({(user1: UserMatches) -> Bool in
            let stringMatch = user1.finalMatchesName.rangeOfString(searchText)
            return (stringMatch != nil)
        })
    }

    func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
        self.filterContentForSearchText(searchString)
        return true
    }

    func createTableView() {
        tableView.sizeToFit()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

        searchBar.sizeToFit()
        searchBar.delegate = self
        searchBar.showsScopeBar = true
        searchBar.searchBarStyle = UISearchBarStyle.Minimal
        searchBar.showsCancelButton = true
        searchBar.returnKeyType = UIReturnKeyType.Search
        tableView.tableHeaderView = searchBar

        var searchDispCont = UISearchDisplayController(searchBar: searchBar, contentsController: nil)
        searchDispCont.delegate = self;
        searchDispCont.searchResultsDataSource = self;
        searchDispCont.searchResultsDelegate = self;

        self.view.addSubview(tableView)
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.searchDisplayController!.searchResultsTableView {  //fatal error: unexpectedly found nil while unwrapping an Optional value
            return self.filterUsers.count
        } else {
            return self.userMatches.count
        }
    }
}

最佳答案

是的,视图控制器的searchDisplayController属性的类型为UISearchDisplayController?(可选),并且在nil内部为tableView(_:numberOfRowsInSection:)。当您使用!强制解开可选内容时,如果值为nil,则您的应用程序将抛出运行时异常(您看到的异常)。

该属性为nil的原因是您通过编程方式创建了该属性,并为nil参数指定了contentsController。这很可能应该是:

var searchDispCont = UISearchDisplayController(searchBar: searchBar, contentsController: self)


这将隐式地将其分配给视图控制器的searchDisplayController属性。

另外,值得注意的是,iOS 8中不推荐使用UISearchDisplayControllerUISearchDisplayDelegate,因此您应该改用UISearchController

关于uitableview - 搜索显示 Controller 导致崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29589621/

10-09 10:17