我有5个标签从tableView
的每个单元格中接收来自SQL Query数组的数据。我想在它上面实现搜索栏。搜索栏应搜索1个特定标签的数据。它正在处理和过滤该特定标签,但在过滤后,其他4个标签不会更新,而且根本不会发生变化。我无法弄明白。由于我只有1个来自搜索栏条目的过滤数据,因此我不知道应该如何过滤其他数据。
谢谢
编辑:仍然无法弄清楚类和添加数据,我有错误
"类型的价值' [String]'没有会员 ' localizedCaseInsensitiveContains'"
在搜索栏功能
上import UIKit
class TableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var data:[MyData] = []
@IBOutlet weak var searchBar: UISearchBar!
var searchActive : Bool = false
var filtered:[MyData] = []
@IBAction func sqlExecute(_ sender: AnyObject) {
var client = SQLClient()
client.connect("sql_ip", username: "username", password: "password", database: "database") {
success in
if success {
client.execute("sql query") {
result in
for table in result as Any as! NSArray {
for row in table as! NSArray {
for column in row as! NSDictionary {
if column.key as! String == "data1" {
MyData.init(data1: "\(column.value)")
} else if column.key as! String == "data2" {
MyData.init(data2: column.value as! Double)
} else if column.key as! String == "data3" {
MyData.init(data3: "\(column.value)")
} else if column.key as! String == "data4" {
MyData.init(data4: "\(column.value)")
} else if column.key as! String == "data5" {
MyData.init(data5: "\(column.value)")
}
}
}
}
client.disconnect()
self.tableView.reloadData()
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count
} else {
return data.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "segue", for: indexPath)
if(searchActive) {
cell.label1.text = "\(filtered[indexPath.row])"
cell.label2.text = "\(filtered[indexPath.row])"
cell.label3.text = "\(filtered[indexPath.row])"
cell.label4.text = "\(filtered[indexPath.row])"
cell.label5.text = "\(filtered[indexPath.row])"
} else {
cell.label1.text = "\(data[indexPath.row])"
cell.label2.text = "\(data[indexPath.row])"
cell.label3.text = "\(data[indexPath.row])"
cell.label4.text = "\(data[indexPath.row])"
cell.label5.text = "\(data[indexPath.row])"
}
cell.backgroundColor = UIColor.clear
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = data.filter { $0.data1.localizedCaseInsensitiveContains(searchText) }
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
这是类文件:
import Foundation
class MyData {
var data1 = [String]()
var data2 = [Double]()
var data3 = [String]()
var data4 = [String]()
var data5 = [String]()
init(data1: String) {
self.data1.append(data1)
}
init(data2: Double) {
self.data2.append(data2)
}
init(data3: String) {
self.data3.append(data3)
}
init(data4: String) {
self.data4.append(data4)
}
init(data5: String) {
self.data5.append(data5)
}
}
答案 0 :(得分:3)
您的问题是您有五个不同的数组,而不是您需要具有The date is 1477785600000000000 and the type is <class 'int'>
The date is 2016-10-30 00:00:00 and the type is <class 'datetime.datetime'>
类型的单个数组,或者可能是自定义public void acquireWakeLock() {
final PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
releaseWakeLock();
//Acquire new wake lock
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
mWakeLock.acquire();
}
public void releaseWakeLock() {
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
mWakeLock = null;
}
}
/ Dictionary
。如果你像这样使用Struct
/ class
,那就太棒了。
struct
现在不是使用五个不同的数组而是一个用于过滤器,而是创建两个class
类型class MyData {
var data1: String!
var data2: Double!
var data3: String!
var data4: String!
var data5: String!
init(data1: String, data2: Double, data3: String, data4: String, data5: String) {
self.data1 = data1
self.data2 = data2
self.data3 = data3
self.data4 = data4
self.data5 = data5
}
}
,其中一个用于显示filterData并将其与Array
方法一起使用并过滤它此
使用[MyData]
方法创建tableView
对象,并将其附加到数据数组,然后像这样过滤你的搜索栏代理。
MyData
上面的过滤器将在数据数组中搜索并返回属性init
包含searchText的所有对象。
您的整个代码就像这样
filtered = data.filter { $0.data1.localizedCaseInsensitiveContains(searchText) }
编辑:您需要像这样使用单个数组。
data1
答案 1 :(得分:0)
首先,您必须创建一个全局数组,然后在搜索栏中委托对其进行排序。
extension HVUserLisitingViewController : UITableViewDelegate , UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filterUser.count
}
}
extension HVUserLisitingViewController : UISearchBarDelegate {
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
return true
}
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
return true }
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return true
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { // called when text changes (including clear)
webserviceSearchBy(text: searchBar.text!)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
filterUser.removeAll()
filterUser = users
tableview.reloadData()
}
func webserviceSearchBy(text : String) {
if text.lowercased().count == 0 {
filterUser.removeAll()
filterUser = users
tableview.reloadData()
return
}
filterUser.removeAll()
filterUser = users.filter({ (user) -> Bool in
if user.username.lowercased().range(of:text.lowercased()) != nil {
return true
}
return false
})
tableview.reloadData()
}
}