无法过滤使用字典数组创建的tableview

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法过滤使用字典数组创建的tableview相关的知识,希望对你有一定的参考价值。

我是swift和Xcode的新手,我一直在使用一个使用用作NSarray的plist字典创建的tableview(具有名字空间姓氏的员工)的应用程序。我正在尝试添加搜索功能。

这是我的代码:

var employeeSearch = Array>()

let searchController = UISearchController(searchResultsController: nil)

func getSwiftArrayFromPlist() -> (Array<Dictionary<String,String>>) {

    let path = Bundle.main.path(forResource: "Sheet1", ofType: "plist")
    var arr: NSArray?
    arr = NSArray(contentsOfFile: path!)
    return (arr as? Array<Dictionary<String,String>>)!
}


func searchBarIsEmpty() -> Bool {
    // Returns true if the text is empty or nil
    return searchController.searchBar.text?.isEmpty ?? true
}

//上述功能没有错误

//我在下面的函数中遇到了一些错误

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    employeeSearch = getSwiftArrayFromPlist().filter { $0["lastname".lowercased().contains(searchText.lowercased()]}
    print(employeeSearch)
    searchActive = !employeeSearch.isEmpty
    tblData.reloadData()
}
//getting many errors in above attempt to get a filtered array of dictionary to reload data in tableview 

任何帮助表示赞赏。谢谢。

// 3月1日 - 最后这个工作,但只有等于条件没有错误。我的期望是包含feature .. func searchBar(_ searchBar:UISearchBar,textDidChange searchText:String){

    if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        employee1View.reloadData()
    } else {
        isSearching = true
        employeeSearch = getSwiftArrayFromPlist()

        currentEmployeeSearch = employeeSearch.filter {($0["lastname"]?.lowercased() == searchText.lowercased())}
        //currentEmployeeSearch = employeeSearch.filter {($0["lastname"]?.lowercased().contains(searchText.lowercased())}
        employee1View.reloadData()
    }
}

// *****************************************

答案

我认为过滤器的括号不正确。我相信

employeeSearch = getSwiftArrayFromPlist().filter { $0["lastname".lowercased().contains(searchText.lowercased()]}

应该

employeeSearch = getSwiftArrayFromPlist().filter { $0["lastname"].lowercased().contains(searchText.lowercased())}

更新了答案以帮助处理选项

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    employeeSearch = getSwiftArrayFromPlist()

    guard !searchText.isEmpty else {
        // In here set the array used in the rest of the app equal to employeeSearch
        return employeeSearch
    }

    currentEmployeeSearch = employeeSearch.filter{
        guard let lowerCasedLastName = $0["lastname"].lowercased() else {
            //If we didn't get a lowercased last name from the employeeSearchArray we return false
            // false means we don't want to include that in the search results array
            return false }
        //If we do have a lowerCasedLastName lets see if it contains the search text
        return lowerCasedLastName.contains(searchText.lowercased())
    }

    employee1View.reloadData()

    //Note: Depending on what array the rest of your application uses you will want to set it equal to the value returned from the filter or returned from the else block of the guard against searchText being empty.
}
另一答案

史蒂夫的解决方案奏效了。非常感谢你! // ********************** func searchBar(_ searchBar:UISearchBar,textDidChange searchText:String){

    if searchBar.text == nil || searchBar.text == "" {
        isSearching = false
        view.endEditing(true)
        employee1View.reloadData()
    } else {
        isSearching = true
        employeeSearch = getSwiftArrayFromPlist()

        //currentEmployeeSearch = employeeSearch.filter {($0["lastname"]?.lowercased() == searchText.lowercased())}
        currentEmployeeSearch = employeeSearch.filter {($0["lastname"]?.lowercased().contains(searchText.lowercased())) ?? false}
        employee1View.reloadData()
    }
}

以上是关于无法过滤使用字典数组创建的tableview的主要内容,如果未能解决你的问题,请参考以下文章

过滤字典数组时无法解析格式字符串?

在 tableView 中显示过滤数组中的数据

ValueError:使用字典时无法将不匹配长度分配给掩码数组

字典数组和字符串的过滤器数组

在 swift 中使用 tableview 和数组过滤器自动完成文本字段

根据字典数组为 tableview 设置 numberOfRowsInSection