我想从在迅速的API值和显示控制器的响应。我创建了表,我正在使用Alarmofire来自API的响应。我的问题是如何使用Alarmofire的成功响应,并在阵列显示器。我的代码是在这里:
class NotificationHistory : UIViewController, UITableViewDelegate,UITableViewDataSource {
let urlStringChecking = "http://www.XXXXXXXX.com/uhf/getnotification.php"
func GetNotificationHistory(completionHandler: @escaping (AnyObject?, NSError?) -> ()) {
getNotifiction(completionHandler: completionHandler)
}
func getNotifiction(completionHandler: @escaping (AnyObject?, NSError?) -> ()){
Alamofire.request(urlStringChecking,method: .get)
.responseJSON{ response in
switch response.result{
case .success:
let list = response.result.value;
completionHandler(list as AnyObject, nil)
case .failure( _):
print(response.result.error as Any)
}
}
}
let list = self.GetNotificationHistory(); // If I get success list here then my problem is solved but I am getting an error.
错误:类型的值 '(NotificationHistory) - >() - >(NotificationHistory)' 没有成员名单'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(list.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = "NotificationCell"
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: reuseIdentifier)
}
cell?.textLabel?.numberOfLines = 0
cell!.textLabel?.text = list[indexPath.row]
cell!.detailTextLabel?.text = list[indexPath.row]
return cell!
}
override func viewDidLoad(){
super.viewDidLoad()
print("Notification History Loaded")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我是很新的雨燕。谢谢
为了处理JSON数据使用SwiftyJSON
使用cocoapods
安装这个第三方库
首先声明你的list
可变NotificationHistory
类作为内
var list = [[String: AnyObject]]()
此代码添加到您完成处理器现在getNotifiction
你的代码看起来象
func getNotifiction(completionHandler: @escaping (AnyObject?, NSError?) -> ()){
Alamofire.request(urlStringChecking,method: .get)
.responseJSON{ response in
switch response.result{
case .success:
if ((response.result.value) != nil){
let jsonData = JSON(response.result.value!)
if let mydata = jsonData["list"].arrayObject {
self.list = mydata as! [[String: AnyObject]]
}
self.tableView.reloadData()
completionHandler(self.list, nil)
}
case .failure( _):
print(response.result.error as Any)
}
}
}
并打电话给你self.GetNotificationHistory()
这里面函数write viewDidLoad()
这一次,我们在list
可变正确的数据,现在是时候来实施tableview
新代码
没有必要在这里改变
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
需要在你的代码的一些变化
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = "NotificationCell"
let newListValue = list[indexPath.row] // assign new values in diff variable
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: reuseIdentifier)
}
cell?.textLabel?.numberOfLines = 0
cell!.textLabel?.text = newListValue[indexPath.row] as? String
cell!.detailTextLabel?.text = newListValue[indexPath.row] as? String
return cell!
}
response.result.value
包含简单的阵列把它当作JSON,我们可以使用它作为
response.result.value as? [[String:String]]
或者您可以使用SwiftyJSON
:d
你可以这样做:
let list = self.GetNotificationHistory();
GetNotificationHistory发出异步请求,不返回任何东西。你应该设置列表,你的价值当你从服务器的响应,然后重新装入您的tableView。
如果名单应该是一个字符串数组,替换为let list = self.GetNotificationHistory()
:
为表=字符串
改变你的GetNotification方法:
func getNotifiction(completionHandler: @escaping ([String]?, NSError?) -> ()){
Alamofire.request(urlStringChecking,method: .get)
.responseJSON{ response in
switch response.result{
case .success:
if let list = response.result.value as? [String] {
self.list = list
}
self.tableView.reloadData()
completionHandler(self.list, nil)
case .failure( _):
print(response.result.error as Any)
}
}
}
然后在您的viewDidLoad中调用self.GetNotificationHistory()
。