当前位置: 首页 > 知识库问答 >
问题:

无法筛选使用字典数组创建的tableview

梁丘赞
2023-03-14

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

这是我的代码:

var employeeSearch=数组

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日-这终于起作用了,但只有相等的条件才不会出错。我的期望是有包含特性。。func searchBar(\uSearchBar: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()
    }
}

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

共有2个答案

窦哲彦
2023-03-14

史蒂夫的解决方案奏效了。非常感谢! //********************** func search chBar(_search chBar: UISearchBar, text Di Change search chText: 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()
    }
}
卫寒
2023-03-14

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

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.
}
 类似资料:
  • 我有一个对象,如下所示: 我想过滤并创建一个满足任何条件的新数组。例如:过滤器的名称为“AD”,并创建一个新的密钥数组,如下所示: 尝试地图 尝试过了。过滤器: 提前谢谢

  • 问题内容: 我试图迅速过滤字典: 上面的过滤器代码在swift 2下编译,但产生以下错误: 无法将类型[[(String,String)]’的值分配给类型’[String:String]’的值 这是swift编译器中的错误,还是不是快速过滤字典的正确方法? 提前非常感谢您! 问题答案: 此问题已在Swift 4中修复 在Swift 4中,过滤后的字典返回字典。 Swift 2和3的原始答案 问题是

  • 我有一个形式的字典: 我想过滤列表,以删除索引中的每一项,其中索引中的不是 输出应如下所示: 因为的第一个值不是 我试过这样的方法: 但这只适用于一把钥匙。 回复:下面有很多好的解决方案。我只是在想另一个。 将这个列表结构转换成一个元组列表: 然后我只需要通过每个元组的第二个项目来过滤这个列表,这很容易:

  • 我有一个返回字符串[][]的方法,如下所示: 我做错了什么?

  • 这就是Firebase里面的样子 这就是我的代码的样子:

  • 问题内容: 我想使用simpleJdbcInsert类和executeBatch方法 http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html 所以我需要传递一个as参数数组。如何创建这样的数组?我试过的是 错误:无法创建通用数组 A