简体   繁体   中英

Search Bar in a TableView with Core Data (using Swift)

I am making a TableViewController with Core Data. In fact the users can add new items to the Table View and these items are saved in Core Data . Everything has worked fine but when I add a Search Bar I'm blocked.

I added a Search Bar but this function bellow create an error when I run the app in the simulator.

func updateSearchResultsForSearchController(searchController: UISearchController,)
{
    filteredTableData.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
    let array = (series as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableData = array as! [String]

    self.tableView.reloadData()
}

The error I get is " terminating with uncaught exception of type NSException "

PS : I use Xcode 6.3.2 and all the code is in Swift.

Try the following:

func updateSearchResultsForSearchController(searchController: UISearchController) {
    var request = NSFetchRequest(entityName: "Serie")
    filteredTableData.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF.infos CONTAINS[c] %@", searchController.searchBar.text)
    let array = (series as NSArray).filteredArrayUsingPredicate(searchPredicate)

    for item in array
    {
        let infoString = item.infos
        filteredTableData.append(infoString)
    }

    self.tableView.reloadData()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM