您好我正在尝试实现一个通过搜索栏搜索/过滤数据的表格视图。这个想法是,对于用户在搜索栏中插入的每个字符(或字符组),应用程序向服务器发送REST查询,数据显示在表格视图中。
问题是表格没有刷新。例如,如果我插入" Marco"在搜索栏中,该表仅在我插入新字符后显示数据。似乎表格视图同步一步。
我的实施是这样的,有什么不对?
class MyViewController: UITableViewController, UISearchBarDelegate {
var queue = NSOperationQueue()
var scrollId: String?
var data: Array<String> = []
override func viewDidLoad()
{
super.viewDidLoad()
queue.maxConcurrentOperationCount = 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
let f = self.data[indexPath.row]
cell.textLabel?.text = f
return cell
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row != data.count - 1) {
return
}
if (scrollId != nil) {
getMoreDataAndRefresh(self.scrollId!)
}
}
internal func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
getFoodAndRefreshTable(searchText);
}
///////////////////////////////////
private func getMoreDataAndRefresh(scrollId:String) -> Void {
let downloadFood = NSBlockOperation(block: {
let wrap:Wrap = self.getFoodAndAwaitTermination(nil, scrollId: self.scrollId)
if wrap.error != nil {
print("Opsss!: \(wrap.error)")
return
}
NSOperationQueue.mainQueue().addOperationWithBlock({
if (wrap.data != nil) {
self.data.appendContentsOf(wrap.data!)
self.tableView.reloadData()
}
})
})
queue.addOperation(downloadFood)
}
private func getFoodAndRefreshTable(searchText:String?) -> Void {
let downloadFood = NSBlockOperation(block: {
let wrap:Wrap = self.getFoodAndAwaitTermination(searchText, scrollId: nil)
if wrap.error != nil {
print("Opsss!: \(wrap.error)")
return
}
NSOperationQueue.mainQueue().addOperationWithBlock({
if self.scrollId != nil {
dataAccess.clearRequest(self.scrollId!)
}
self.scrollId = wrap.scrollId;
if (wrap.data != nil) {
self.data.removeAll()
self.data.appendContentsOf(wrap.data!)
}
self.tableView.reloadData()
})
})
queue.addOperation(downloadFood)
}
private func getFoodAndAwaitTermination(text:String?, scrollId:String?) -> Wrap {
let semaphore = dispatch_semaphore_create(0);
var s:String?
var r:Array<String> = []
var e:String?
dataAccess.getAllFood(text, scrollId : scrollId, pageSize: 20) { (result, scrollId, error) in
if (error != nil ) {
e = error!
} else {
s = scrollId!
r.appendContentsOf(result!)
}
dispatch_semaphore_signal(semaphore);
}
let timeout = dispatch_time(DISPATCH_TIME_NOW, 20000000000)
let res = dispatch_semaphore_wait(semaphore, timeout);
return Wrap(food: r, scrollId: s, error: e)
}
class Wrap {
var data:Array<String>?
var scrollId:String?
var error:String?
init(food:Array<String>?, scrollId:String?, error:String?) {
self.data = data
self.scrollId = scrollId
self.error = error
}
}
答案 0 :(得分:0)
您面临的主要问题是,您已经在代码下面使用了搜索栏的textDidChagned委托,而且它适用于您。
干杯!!
func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
var textFieldString : String = ""
if let searchText = searchBar.text {
textFieldString = searchText+text
} else {
textFieldString = text
}
getFoodAndRefreshTable(textFieldString)
}