未从 Web 服务获取 UITableView 中的数据
Not getting data in UITableView from web service
我对处理 swift 中的请求和响应相当陌生。这是我坚持使用的代码。我从 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()
}
}
}
}
}
不要忘记为您的 tableView
添加 delegate
和 dataSource
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.tableView.delegate = self
self.tableView.dataSource = self
callData()
}
然后在获取数据后重新加载 tableView:
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)
self.tableView.reloadData() // you missing this method
}
}
}
}
您需要更改 tableview.reloadData() 调用。一旦响应在 api 调用之后出现并且您将响应存储在 dataArray
中,您就可以重新加载 table。
因此,在您的情况下,在 viewDidLoad() 中重新加载 tableview 没有意义,因为您的 dataArray
是空的。
所以请像这样更改callData()
方法
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)
}
}
}
}
我对处理 swift 中的请求和响应相当陌生。这是我坚持使用的代码。我从 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()
}
}
}
}
}
不要忘记为您的 tableView
delegate
和 dataSource
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.tableView.delegate = self
self.tableView.dataSource = self
callData()
}
然后在获取数据后重新加载 tableView:
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)
self.tableView.reloadData() // you missing this method
}
}
}
}
您需要更改 tableview.reloadData() 调用。一旦响应在 api 调用之后出现并且您将响应存储在 dataArray
中,您就可以重新加载 table。
因此,在您的情况下,在 viewDidLoad() 中重新加载 tableview 没有意义,因为您的 dataArray
是空的。
所以请像这样更改callData()
方法
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)
}
}
}
}