gpt4 book ai didi

ios - 未从 Web 服务获取 UITableView 中的数据

转载 作者:搜寻专家 更新时间:2023-11-01 06:32:07 24 4
gpt4 key购买 nike

我对快速处理请求和响应相当陌生。这是我坚持使用的代码。我从 dataArray 中的 web 服务获取值,但没有加载到 tableview 中。请有人帮忙。

import UIKit
import Foundation
import Alamofire
import SwiftyJSON

class AddOnsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var dataArray = [AnyObject]()
var addOnsURL = "http://econstrademosite.com/food_delivery/?****************"

override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.tableView.reloadData()
callData()
}

@IBAction func goBackToHome(sender: UIBarButtonItem){

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "base") as! SWRevealViewController
self.present(vc, animated: true, completion: nil)
}

func callData(){

Alamofire.request(addOnsURL).responseJSON { response in

let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject>{
if let innerDict = dict["data"]?["result"]{
self.dataArray = innerDict as! [AnyObject]
print(self.dataArray)
}
}
}
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(dataArray.count)
return dataArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! exampleTableViewCell
let itemName = dataArray[indexPath.row]["item_name"]
cell.label.text! = itemName as! String

return cell
}

}

最佳答案

    func callData(){

Alamofire.request(addOnsURL).responseJSON { response in

let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject>{
if let innerDict = dict["data"]?["result"]{
self.dataArray = innerDict as! [AnyObject]
print(self.dataArray)
if self.dataArray.count > 0
{
self.tableView.reloadData()
}
}
}
}
}

关于ios - 未从 Web 服务获取 UITableView 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45751182/

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