【问题标题】:Make UISearchBar to search on two different fields使 UISearchBar 在两个不同的字段上进行搜索
【发布时间】:2015-09-01 07:14:34
【问题描述】:

我正在使用 UISearchBarUITableView 的单元格中搜索某些文本。我正在从对象数组中填充这些单元格的数据。这些对象每个都有三个不同的属性,其中两个是String 类型,而第三个是UIImage。有什么方法可以让 UISearchBar 在对象的两个文本字段上搜索文本,而无需询问用户他们希望在哪个字段上执行搜索?

【问题讨论】:

    标签: ios swift uitableview uisearchbar


    【解决方案1】:

    有一种简单的方法可以过滤数组,您可以完全决定数组的对象何时应该在新数组中:

    data = data.filter({( aObject: YourClass) -> Bool in
    
    // code for checking if the aObject should be in the data array
    // then return true if it should be in the new array and false if not
    // For example your return statement could be something like this:
    
    return (aObject.firstString.lowercaseString.rangeOfString(searchText.lowercaseString) != nil) || (aObject.secondString.lowercaseString.rangeOfString(searchText.lowercaseString) != nil)
    })
    

    【讨论】:

    • 简单而精彩.. 谢谢
    • 从未尝试过朝那个方向思考。我正在考虑创建一个包含所有字符串的新数组,然后以某种方式将其与原始数组链接。你为我节省了几个小时
    • 只有一个问题.. 不应该是|| 而不是&& ;)
    • 你能详细说明一下吗?我是 Swift 的初学者。我真的不知道该怎么做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2014-08-10
    • 2013-04-07
    • 1970-01-01
    • 2018-08-30
    相关资源
    最近更新 更多