gpt4 book ai didi

json - 使用 AlamoFire 获取数据以根据 UISearchBar 值填充 UITableView

转载 作者:行者123 更新时间:2023-11-28 15:37:28 24 4
gpt4 key购买 nike

与许多 JSON 请求不同,我打算根据输入到搜索栏中的内容发送一个 JSON 请求(使用 AlamoFire),而不是过滤一个长的预先确定的 JSON,从而获取要解析的 JSON(使用 SwiftyJSON)填充 UITableView

enter image description here

我想做的是使用 searchTerm(在搜索栏中输入)向 Geonames 发送 AlamoFire 请求在 geonames API 请求的查询字段中使用 searchTerm。即

http://api.geonames.org/searchJSON?formatted=true&q=(searchTerm)&maxRows=3&username=demo

我基本上不能做的是在填写搜索栏后发送请求,然后用该数据填充表格。

下面是我当前的代码(它在加载 View 时使用解析的 JSON 数据填充表格,并请求 “burpham”

import UIKit
import Alamofire
import SwiftyJSON

class SearchAddLocationViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{

@IBOutlet weak var tblJSON: UITableView!

@IBOutlet weak var searchbarValue: UISearchBar!

weak open var delegate: UISearchBarDelegate?

var arrRes = [[String:AnyObject]]() //Array of dictionary

override func viewDidLoad()
{
super.viewDidLoad()
}

public func searchBarTextDidEndEditing(_ 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.
}

}

最佳答案

您需要监听 UISearchBar 委托(delegate)的 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) 方法来填充您的 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
}
}

更新 #1

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.
}

}

关于json - 使用 AlamoFire 获取数据以根据 UISearchBar 值填充 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44134299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com