嗨我試圖實現一個表格視圖,通過搜索欄搜索/過濾數據。這個想法是,對於用戶在搜索欄中插入的每個字符(或一組字符),應用程序會向服務器發送一個REST查詢,並將數據顯示在表視圖中。Swift 2 - 通過搜索欄過濾REST服務和TableView
問題是表不刷新。例如,如果我在搜索欄中插入「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
}
}
我無法找到searchBar/search code?你能補充一點嗎? –