2016-07-26 89 views
1

嗨我試圖實現一個表格視圖,通過搜索欄搜索/過濾數據。這個想法是,對於用戶在搜索欄中插入的每個字符(或一組字符),應用程序會向服務器發送一個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 
    } 
} 
+0

我無法找到searchBar/search code?你能補充一點嗎? –

回答

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) 
    } 
+0

Tnx它的工作原理。其實我用這個:'textFieldString =(searchText as NSString).stringByReplacingCharactersInRange(range,withString:text)' –