如何在文本字段编辑中过滤tableview数据 - IOS

时间:2017-05-26 11:37:45

标签: ios iphone uitableview swift3 xcode8.1

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

2 个答案:

答案 0 :(得分:0)

我的建议是使用 UISearchController ,它具有很多功能,如果你使用它,你现在要求的是如此简单。 所以请去UISearchController。

UISearchController tutorial会对您有所帮助。

答案 1 :(得分:0)

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

和网站链接可以解决您的问题 http://www.appcoda.com/custom-search-bar-tutorial/

你只需要在编辑功能时继续重新加载表并输入代码。

例如:

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