Json 使用AlamoFire获取数据以基于UISearchBar值填充UITableView
与许多JSON请求不同,我不想过滤一个长的预先确定的JSON,而是打算根据输入到搜索栏中的内容使用AlamoFire发送JSON请求,从而获取要用SwiftyJSON解析的JSON以填充UITableView 我想做的是使用输入到搜索栏中的searchTerm向geonames API请求的查询字段中的searchTerm发送AlamoFire请求。i、 e 我基本上不能做的是在填写搜索栏后发送请求,因此用该数据填充表 下面是我当前的代码,它在视图加载解析的JSON数据并请求burpham时填充表 您需要收听用于填充UITableView的UISearchBar委托的func searchBartextdidediting_searchBar:UISearchBar方法 更新1Json 使用AlamoFire获取数据以基于UISearchBar值填充UITableView,json,swift,uitableview,uisearchbar,Json,Swift,Uitableview,Uisearchbar,与许多JSON请求不同,我不想过滤一个长的预先确定的JSON,而是打算根据输入到搜索栏中的内容使用AlamoFire发送JSON请求,从而获取要用SwiftyJSON解析的JSON以填充UITableView 我想做的是使用输入到搜索栏中的searchTerm向geonames API请求的查询字段中的searchTerm发送AlamoFire请求。i、 e 我基本上不能做的是在填写搜索栏后发送请求,因此用该数据填充表 下面是我当前的代码,它在视图加载解析的JSON数据并请求burpham时填充
import UIKit
import Alamofire
import SwiftyJSON
class SearchAddLocationViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate
{
@IBOutlet weak var tblJSON: UITableView!
@IBOutlet weak var searchbarValue: UISearchBar!
var arrRes = [[String:AnyObject]]() //Array of dictionary
override func viewDidLoad()
{
super.viewDidLoad()
self.searchbarValue.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { // called when text ends editing
{
callAlamo(searchTerm: searchbarValue.text!)
}
func callAlamo(searchTerm: String)
{
Alamofire.request("http://api.geonames.org/searchJSON?q=\(searchTerm)&maxRows=5&username=garethallenstringer").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["geonames"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.tblJSON.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "addLocProtoCell")!
var dict = arrRes[indexPath.row]
cell.textLabel?.text = dict["name"] as? String
cell.detailTextLabel?.text = dict["countryName"] as? String
return cell
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arrRes.count
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
因此,我从中看到SearchBartextdendediting将实际的UISearchBar作为参数,这是否意味着用户在搜索栏中键入的任何字符串都属于该类型?func SearchBartextdendediting_searchBar:UISearchBar{}将在用户完成对搜索栏的编辑后调用。UISearchBar有一个属性文本,因此您可以像这样获取searchBar当前文本:func searchBartextdendediting_searchBar:UISearchBar{printsearchBar.text},所以为了添加UISearchBarDelegate,必须在顶部有“弱打开变量委托:UISearchBarDelegate?”以及其他初始值设定项/输出项?我相信我已经正确地添加了这些方法,但是当按下软件键盘上的搜索按钮时,不会发生任何事情,尽管调用了名为callAlamo的函数,该函数从searchBarDidEndEditing函数中获取字符串searchBar.text。我将更新我的问题以反映代码中的更改。不,您不需要在代码中包含弱open var delegate:UISearchBarDelegate。只需在viewDidLoad中订阅您的searchbarValue的UISearchBarDelegate,如下所示:self.searchbarValue.delegate=self这是否意味着在初始化类时需要UISearchBarDelegate以及UITableViewDataSource和UITableViewDelegate?我以前作为UITableView的代表使用过它-这不正确吗?
class ViewController: UIViewController, UISearchBarDelegate {
@IBOutlet private weak var searchBar: UISearchBar?
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar?.delegate = self
}
//MARK: UISearchBarDelegate methods
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
//You can do your request via Alamofire to populate tableView
}
}
import UIKit
import Alamofire
import SwiftyJSON
class SearchAddLocationViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate
{
@IBOutlet weak var tblJSON: UITableView!
@IBOutlet weak var searchbarValue: UISearchBar!
var arrRes = [[String:AnyObject]]() //Array of dictionary
override func viewDidLoad()
{
super.viewDidLoad()
self.searchbarValue.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { // called when text ends editing
{
callAlamo(searchTerm: searchbarValue.text!)
}
func callAlamo(searchTerm: String)
{
Alamofire.request("http://api.geonames.org/searchJSON?q=\(searchTerm)&maxRows=5&username=garethallenstringer").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["geonames"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.tblJSON.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "addLocProtoCell")!
var dict = arrRes[indexPath.row]
cell.textLabel?.text = dict["name"] as? String
cell.detailTextLabel?.text = dict["countryName"] as? String
return cell
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arrRes.count
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}