tableView查询中的searchBar(Swift)

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

我真的不知道如何解释这就是为什么我要把这个头衔。但我希望你能理解。截至目前,我的searchBar能够返回结果但我必须输入整个名称(例如:我必须输入“EcoBooth”才能让searchBar返回EcoBooth结果。)。如果我只是输入“Eco”,我将如何制作它,它将基于我键入的几个字符串返回EcoBooth?

P.S:请查看附加的2张图片以获取更多信息。

我的TableView控制器代码

导入UIKit导入FirebaseDatabase

var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()
var postData2 = [String]()
var currentpostDataArray = [String]()
var tableDataArray = [tableData]()
var searchArray = [tableData]()
var inSearchMode = false

class TableViewController: UITableViewController, UISearchBarDelegate {


    @IBOutlet var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpSearchBar()


        ref = Database.database().reference() //set the firebase reference

        // Retrieve the post and listen for changes
        databaseHandle = ref?.child("Posts2").observe(.value, with: { (snapshot) in
            // Code to execute when a child is added under "Posts"
            postData.removeAll()
            postData2.removeAll()
            tableDataArray.removeAll()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key
                let value = String(describing: snap.value!)
                let rating = (value as NSString).integerValue   

                postData.append(key)
                postData2.append(value)

                tableDataArray.append(tableData(boothName: key, boothRating: rating))
                currentpostDataArray = postData
            }

            self.tableView.reloadData()


        })

    }


    private func setUpSearchBar() {
        searchBar.delegate = self
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return currentpostDataArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = currentpostDataArray[indexPath.row]
        cell.detailTextLabel?.text = postData2[indexPath.row] + " ♥"
        cell.detailTextLabel?.textColor = UIColor.red;
        return cell
    }




    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchArray.removeAll()
        searchArray = tableDataArray
        currentpostDataArray.removeAll()
        postData2.removeAll()
        if !searchText.isEmpty {
           for data in searchArray {
            let item = data.boothName
            if (searchText.lowercased().range(of: item) != nil) || data.boothName.lowercased() == searchText.lowercased() ||                 searchText.lowercased().contains(data.boothName.lowercased())
               {
                    currentpostDataArray.append(data.boothName)
                    let value = String(describing: data.boothRating)
                    postData2.append(value)
                }
            }
        }
        if searchText.isEmpty {
            loadDara()
        }
        self.tableView.reloadData()
    }



    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        switch selectedScope {
        case 0:
            currentpostDataArray.removeAll()
            postData2.removeAll()
            for data in tableDataArray {
                currentpostDataArray.append(data.boothName)

                let value = String(describing: data.boothRating)
                postData2.append(value)
            }
            self.tableView.reloadData()

        case 1:
            currentpostDataArray.removeAll()
            postData2.removeAll()
            let sortedTableData = tableDataArray.sorted(by: { $0.boothRating > $1.boothRating })
            for data in sortedTableData {
                currentpostDataArray.append(data.boothName)

                let value = String(describing: data.boothRating)
                postData2.append(value)
            }
            self.tableView.reloadData()

        default:
            break

        }
        tableView.reloadData()
    }

    func loadDara() {
        for data in tableDataArray {
            currentpostDataArray.append(data.boothName)

            let value = String(describing: data.boothRating)
            postData2.append(value)
        }

    }

}

class tableData {
    var boothName: String
    var boothRating: Int
    init(boothName: String, boothRating: Int) {
        self.boothName = boothName
        self.boothRating = boothRating
    }
}

enter image description here enter image description here

ios swift xcode uitableview
2个回答
1
投票

更换

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchArray.removeAll()
    searchArray = tableDataArray
    currentpostDataArray.removeAll()
    postData2.removeAll()
    if !searchText.isEmpty {
       for data in searchArray {
        let item = data.boothName
        if (searchText.lowercased().range(of: item) != nil) || data.boothName.lowercased() == searchText.lowercased() ||                 searchText.lowercased().contains(data.boothName.lowercased())
           {
                currentpostDataArray.append(data.boothName)
                let value = String(describing: data.boothRating)
                postData2.append(value)
            }
        }
    }
    if searchText.isEmpty {
        loadDara()
    }
    self.tableView.reloadData()
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchArray.removeAll()
    searchArray = tableDataArray
    currentpostDataArray.removeAll()
    postData2.removeAll()
    if !searchText.isEmpty {
       for data in searchArray {
        let item = data.boothName
        if (item.lowercased().range(of: searchText.lowercased()) != nil)
           {
                currentpostDataArray.append(data.boothName)
                let value = String(describing: data.boothRating)
                postData2.append(value)
            }
        }
    }
    if searchText.isEmpty {
        loadDara()
    }
    self.tableView.reloadData()
}

0
投票

使用NSPredicate从数组中搜索,如下所示:

let searchPredicate = NSPredicate(format: "boothName CONTAINS[C] %@", searchText)

resultArr = (filteredArray as NSArray).filtered(using: searchPredicate)

您可以通过此链接获取有关NSPredicate的更多信息以及快速代码示例:http://nshipster.com/nspredicate/

© www.soinside.com 2019 - 2024. All rights reserved.