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

问题描述 投票:0回答:2

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

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

ios arrays swift filter uisearchcontroller
2个回答
0
投票

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

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.
}

0
投票

史蒂夫的解决方案奏效了。非常感谢你! // ********************** 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()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.