如何在编辑文本字段时过滤表格视图数据 - IOS

How to filter tableview data on textfield editing - IOS

我使用数组和一个文本字段创建了 table视图。 在文本字段编辑中table视图数据将根据文本字段输入进行排序和过滤。如果table视图有关于状态的数据,如果我输入'A' 在文本字段中,所有 table 视图数据将被排序并显示所有以字母 'A' 开头的州名称。请告诉我这个的逻辑。

我的建议是使用 UISearchController ,它有很多功能,如果你使用它,你现在要问的就是这么简单。 所以请使用 UISearchController。

UISearchController tutorial 会对你有所帮助。

这个 Youtube 视频:https://www.youtube.com/watch?v=MgNRMcCWJhU&list=PLt2cCXacqzgfUAjHYnZ9rrPkih4NzAV4E&index=25

和站点 link 可能会解决您的问题。
http://www.appcoda.com/custom-search-bar-tutorial/

你只需要不断重新加载table并在编辑函数时输入代码。

例如:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    shouldShowSearchResults = true
    tblSearchResults.reloadData()
}



func updateSearchResultsForSearchController(searchController: UISearchController) {
    let searchString = searchController.searchBar.text

    // Filter the data array and get only those countries that match the search text.
    filteredArray = dataArray.filter({ (country) -> Bool in
        let countryText: NSString = country

        return (countryText.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
    })

    // Reload the tableview.
    tblSearchResults.reloadData()
}