【问题标题】:How to filter tableview data on textfield editing - IOS如何在文本字段编辑中过滤 tableview 数据 - IOS
【发布时间】:2017-05-26 11:37:44
【问题描述】:

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

【问题讨论】:

  • 您可以借助 Predicate 过滤掉结果,然后重新加载您的表。注意:保留两个数组,一个将包含所有数据,另一个将保存来自第一个数组的过滤数据。希望对你有帮助

标签: ios iphone uitableview swift3 xcode8.1


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    UISearchController tutorial会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 2019-11-09
      • 2021-08-23
      • 1970-01-01
      相关资源
      最近更新 更多