searchBar在tableView查询(斯威夫特)

我真的不知道如何解释这就是为什么我把这个标题。但我希望你能理解。截至目前,我的searchBar能够返回结果,但我必须输入完整的名称(例如:为了让searchBar返回EcoBooth结果,我必须键入“EcoBooth”)。 我怎样才能让它仿佛如果我只输入“生态”,它会基于我输入的几个字符串返回EcoBooth?searchBar在tableView查询(斯威夫特)

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

}

}

回答:

更换

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()

}

随着迅速filteredArrayUsingPredicate

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()

}

回答:

使用NSPredicate从阵列搜索这样的:

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

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

你可以喜欢这个链接了解更多信息关于NSPredicate也迅速代码示例:http://nshipster.com/nspredicate/

以上是 searchBar在tableView查询(斯威夫特) 的全部内容, 来源链接: utcz.com/qa/261182.html

回到顶部