选中时UISearchController崩溃

时间:2016-06-06 18:26:24

标签: ios swift uisearchbar uisearchcontroller

我尝试在我的ViewController中实现一个UISearchController。但是,当我尝试从按钮打开Controller时,它会使应用程序崩溃,并且Xcode没有给出错误。我使用Backendless来存储用户并检索它们。我想搜索它们。 这是我的代码。我不知道自己做错了什么:/我真的很感激帮助。

import UIKit

protocol ChooseUserDelegate {
func chreatChatroom(withUser: BackendlessUser)
}

class ChooseUserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {

@IBOutlet weak var tableView: UITableView!


var delegate: ChooseUserDelegate!
var users: [BackendlessUser] = []
var filteredUsers = [BackendlessUser]()
var resultSearchController: UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()

    loadUsers()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


//MARK: UITableviewDataSorce
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func updateSearchResultsForSearchController(searchController: UISearchController) {

            self.filteredUsers.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!);    let array = (self.users as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredUsers = array as! [BackendlessUser]

    self.tableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if resultSearchController.active
    {
    return self.filteredUsers.count
    } else {
    return users.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell?

    if self.resultSearchController.active
    {
        let userFiltered = filteredUsers[indexPath.row]
        cell!.textLabel?.text = userFiltered.name
    } else {
        let user = users[indexPath.row]
        cell!.textLabel?.text = user.name
    }

    return cell!
}


//MARK: UITableviewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let user = users[indexPath.row]

    delegate.chreatChatroom(user)

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    self.dismissViewControllerAnimated(true, completion: nil)
}


//MARK: IBactions
@IBAction func cancelButtonPressed(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

//MARK: Load Backendless Users

func loadUsers() {

    let whereClause = "objectId != '\(backendless.userService.currentUser.objectId)'"

    let dataQuery = BackendlessDataQuery()
    dataQuery.whereClause = whereClause

    let dataStore = backendless.persistenceService.of(BackendlessUser.ofClass())
    dataStore.find(dataQuery, response: { (users : BackendlessCollection!) -> Void in

        self.users = users.data as! [BackendlessUser]
        self.resultSearchController = UISearchController(searchResultsController: nil)
        self.resultSearchController.searchResultsUpdater = self

        self.resultSearchController.dimsBackgroundDuringPresentation = false
        self.resultSearchController.searchBar.sizeToFit()

        self.tableView.tableHeaderView = self.resultSearchController.searchBar

        self.tableView.reloadData()


        }) { (fault : Fault!) -> Void in
            print("Error, couldnt retrieve users: \(fault)")
    }

}

}

1 个答案:

答案 0 :(得分:2)

resultSearchController nil使用之前viewDidLoad()。它设置在一个闭包中,在设置视图时,几乎可以确定之后需要它。要解决此问题,只需将此初始化代码移至早期代码self.resultSearchController = UISearchController(searchResultsController: nil) self.resultSearchController.searchResultsUpdater = self self.resultSearchController.dimsBackgroundDuringPresentation = false self.resultSearchController.searchBar.sizeToFit() self.tableView.tableHeaderView = self.resultSearchController.searchBar

即可
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($FilePath)