代码之家  ›  专栏  ›  技术社区  ›  Ümañg ßürmån

搜索自定义UITableCell的项目并使用Swift 4进行筛选

  •  0
  • Ümañg ßürmån  · 技术社区  · 6 年前

    我正在尝试在UITableViewController中实现搜索功能。所以我在UITableView中的单元格顶部添加了一个搜索控制器。

    我做过这个 UISearchBarDelegate 用我的TableViewController。

    以下是我过滤搜索文本的代码:

    // This method updates filteredData based on the text in the Search Box
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        // When there is no text, filteredData is the same as the original data
        // When user has entered text into the search box
        // Use the filter method to iterate over all items in the data array
        // For each item, return true if the item should be included and false if the
        // item should NOT be included
        filteredData = searchText.isEmpty ? OrderDetailsTabBarViewController.orderDetailsList : OrderDetailsTabBarViewController.orderDetailsList.filter { (item: OrderBookedSetterGetter) -> Bool in
            // If dataItem matches the searchText, return true to include it
            return item.BookedOrderId(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
        }
    
        tableView.reloadData()
    }
    

    但是,它显示了错误 无法调用非函数类型“Int”的值 返回项目.BookedOrderId(共:searchText,选项:。不区分大小写,范围:nil,区域设置:nil)!=无

    预订订单 是Int

    谁能帮我一下吗。我困在这里什么也找不到。

    Link Here

    谢谢您。

    2 回复  |  直到 6 年前
        1
  •  1
  •   teja_D    6 年前

    我就是这么做的,而且很管用!

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            // When there is no text, filteredData is the same as the original data
            // When user has entered text into the search box
            // Use the filter method to iterate over all items in the data array
            // For each item, return true if the item should be included and false if the
            // item should NOT be included
    
    
                self.searchBar.showsCancelButton = true
    
                self.copied_order_Canceled_DetailList = []
                for item in self.order_Canceled_DetailList {
                if String(item.orderId).localizedCaseInsensitiveContains(searchText) != false{
                   self.copied_order_Canceled_DetailList.append(item))
                } else {
    
                    }
                }
    
                self.tableview.reloadData()
                if searchText == "" {
                    self.copied_order_Canceled_DetailList = self.order_Canceled_DetailList
                    self.tableview.reloadData()
                }
            }
    

    这应该管用。

        2
  •  1
  •   vadian    6 年前

    range(of... Int String

    filteredData = searchText.isEmpty ? OrderDetailsTabBarViewController.orderDetailsList : OrderDetailsTabBarViewController.orderDetailsList.filter { (item: OrderBookedSetterGetter) -> Bool in
        // If dataItem matches the searchText, return true to include it
        let stringOrderId = String(item.BookedOrderId)
        return stringOrderId.range(of: searchText, options: .caseInsensitive) != nil
    }