简体   繁体   English

具有核心数据的TableView中的搜索栏(使用Swift)

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

I am making a TableViewController with Core Data. 我正在用核心数据制作一个TableViewController In fact the users can add new items to the Table View and these items are saved in Core Data . 实际上,用户可以将新项目添加到表视图中,并且这些项目将保存在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 " 我得到的错误是“ 以类型为NSException的未捕获异常终止

PS : I use Xcode 6.3.2 and all the code is in Swift. PS:我使用Xcode 6.3.2,所有代码都在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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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