当搜索栏处于活动状态时,不会填充swift + tableview数据

时间:2016-02-14 14:18:26

标签: json swift search tableview

当我加载表格然后激活搜索时,我可以在tableview中获取搜索数据。为了使我的应用程序更快,我创建了一个仅用于搜索的单独视图。但我无法通过搜索数据填充我的tableview。我可以看到我使用print(数据)正确获取数据,但是我的表没有填充。

     class YazarTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {

        var searchResults: [JSON]? = []
        var searchController = UISearchController()
        @IBOutlet weak var searchButton: UIBarButtonItem!
        @IBAction func searchButtonTouched(sender: AnyObject) {
           showSearchBar()
        }

        func showSearchBar() {   
            self.searchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.searchBar.delegate = self
            controller.dimsBackgroundDuringPresentation = true
            controller.searchBar.alpha = 0
            navigationItem.setLeftBarButtonItem(nil, animated: true)
            UIView.animateWithDuration(0.5, animations: {
               controller.searchBar.alpha = 1
                }, completion: { finished in
                    controller.searchBar.becomeFirstResponder()
            })

            self.tableView.tableHeaderView = controller.searchBar

            return controller
        })()
        }
      }


      func updateSearchResultsForSearchController(searchController: UISearchController)
                {
                    self.searchResults?.removeAll(keepCapacity: false)
                    if (!searchController.searchBar.text!.isEmpty){
                        let parameters = ["q" : searchController.searchBar.text]
                        var searchURL = "http://apilink.com/" + String(parameters) + "/1"
                        searchURL = searchURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
                            Alamofire.request(.GET, searchURL, encoding: .JSON)
                            .responseJSON{ response in
                                guard response.result.error == nil else {
                                    // got an error in getting the data, need to handle it
                                    print("error calling GET on /posts/1")
                                    print(response.result.error!)
                                    return
                                }
                                 if let value: AnyObject = response.result.value {
                                    let post = JSON(value)
                                    if let data = post[].arrayValue as [JSON]?{
                                        self.searchResults = data
                                        self.tableView.reloadData()
                                        print(data)
                                    }
                                }
                        }
                    }

                }

0 个答案:

没有答案