Swift - 具有多个选择的搜索表视图

时间:2015-11-23 23:19:00

标签: xcode swift uitableview search ios9

我已经按照Vea软件教程(https://www.veasoftware.com/tutorials/2015/6/27/uisearchcontroller-in-swift-xcode-7-ios-9-tutorial)在xcode 7,ios 9上创建了带有搜索栏的表格视图。现在我需要在表格视图中选择多行,方法是在每个行上添加一个复选标记。选择行时,问题是当我使用搜索栏时,复选标记不再与行匹配..

这是我的代码:

class SportSearchTableViewController: UITableViewController, UISearchResultsUpdating {

let appleProducts = ["Mac","iPhone","Apple Watch","iPad"]
var filteredAppleProducts = [String]()
var resultSearchController = UISearchController()

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath,animated: true)
    let selectedRow = tableView.cellForRowAtIndexPath(indexPath)!
    if selectedRow.accessoryType == UITableViewCellAccessoryType.None {
        selectedRow.accessoryType = UITableViewCellAccessoryType.Checkmark
        selectedRow.tintColor = UIColor.blueColor()
    } else {
        selectedRow.accessoryType = UITableViewCellAccessoryType.None
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    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()
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell?

    if (self.resultSearchController.active)
    {
        cell!.textLabel?.text = self.filteredAppleProducts[indexPath.row]

        return cell!
    }
    else
    {
        cell!.textLabel?.text = self.appleProducts[indexPath.row]

        return cell!
    }
}


func updateSearchResultsForSearchController(searchController: UISearchController)
{
    self.filteredAppleProducts.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (self.sportsList as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredAppleProducts = array as! [String]

    self.tableView.reloadData()
}

有谁知道如何解决这个问题?

0 个答案:

没有答案