尝试使用 "search" 方法,但我仍然有错误

trying to use the "search" method but I still have an error

嗨,这是我第一次尝试使用搜索方法,我使用的是 Apple Developer 网站上的搜索栏指南,但我遇到了这个问题,似乎无法解决。我的数据存储在 Class DataServices 中,我的 tableView 单元格在我的 ViewController 中工作得非常完美,但我似乎无法弄清楚我应该 return搜索方法完成该方法,当我按照搜索指南说明进行操作时 Xcode 对我尖叫。你能用你的知识祝福我吗

class ListVC: UIViewController,UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!

    @IBOutlet weak var tableView: UITableView!

    var filteredData: [List]!
    let data = DataServices.instance.getList()

    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.delegate = self
        tableView.dataSource = self
        tableView.dataSource = self
        filteredData = data
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return DataServices.instance.getList().count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "ListingCell") as? ListCell {
            let listing = DataServices.instance.getList()[indexPath.row]
            cell.updateView(lists: listing)
            return cell
        }else {
            return ListCell()
        }
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filteredData = searchText.isEmpty ? data: data.filter({ (List: String) -> Bool in
            return List.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil    

> Value of type 'List' has no member 'range' is the error I get

        })

        tableView.reloadData()
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        self.searchBar.showsCancelButton = true
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = false
        searchBar.text = ""
        searchBar.resignFirstResponder()

    }

}

class数据服务{ 静态让实例 = DataServices()

private let currentList = [
    List(firstText: "Name", secondText: "Shannon"),
    List(firstText: "Car", secondText: "Infinity"),
    List(firstText: "City", secondText: "Brandon"),
    List(firstText: "Wife", secondText: "Naedra"),
    List(firstText: "Child", secondText: "Shantae"),
    List(firstText: "Job", secondText: "Driver"),
    List(firstText: "Cell", secondText: "iPhone"),
    List(firstText: "Computer", secondText: "Imac")

]

func getList() -> [List] {
return currentList
}

}

结构列表{

private (set) public var toptitle: String
private (set) public var bottomtitle: String

init(firstText: String, secondText: String) {
    self.toptitle = firstText
    self.bottomtitle = secondText
}

}

闭包参数表示实例而不是类型,应用于数组的 filter 闭包只接受一个参数。检查空字符串是多余的。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filteredData = data.filter ({ (list) -> Bool in
        return list.firstText.range(of: searchText, options: .caseInsensitive) != nil ||
               list.secondText.range(of: searchText, options: .caseInsensitive) != nil
    })
    tableView.reloadData()
}

或使用简化语法

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filteredData = data.filter { [=11=].firstText.range(of: searchText, options: .caseInsensitive) != nil ||
                                 [=11=].secondText.range(of: searchText, options: .caseInsensitive) != nil }  
    tableView.reloadData()
}

我建议将 filteredData 声明为非可选空数组以避免不必要的展开

var filteredData = [List]()

并强制向下转换单元格,如果代码崩溃,则表明存在设计错误。

let cell = tableView.dequeueReusableCell(withIdentifier: "ListingCell", for: indexPath) as! ListCell

如果你想在你的 List 结构中使用常量,只需写

struct List {
    let toptitle: String
    let bottomtitle: String
}

您可以免费获得初始值设定项。 private (set) var 非常 objective-c-ish

最终在每次调用 cellForRow 时获得相同的(静态)列表是不必要的昂贵,从 data

中获取项目
let listing = data[indexPath.row]