gpt4 book ai didi

ios - swift 3/Alamofire : Cant load the table with the data

转载 作者:搜寻专家 更新时间:2023-10-31 22:41:55 24 4
gpt4 key购买 nike

我正在尝试在 UItableview 上加载数据,我能够在输出面板中看到获取的记录,但无法在 UItableview 上获取它们。

请在下面找到代码:

import UIKit
import Alamofire
import SwiftyJSON


class CategoryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSURLConnectionDelegate {

var tableData = Array<Category>()
var arrRes = [[String:AnyObject]]() //Array of dictionary
var category = [Category]()
var catTitle = ""
var id = ""



@IBOutlet weak var catTitleRec: UILabel!

@IBOutlet weak var tableview: UITableView!




override func viewDidLoad() {
super.viewDidLoad()

catTitleRec.text = catTitle
loadCategories(id:id)
}

func loadCategories(id : String) {


let userDic : [String : AnyObject] = ["id":id as AnyObject]

Alamofire.upload(multipartFormData: { multipartFormData in

for (key, value) in userDic {
multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
}

},to: "http://www.URL.com.au/database/results.php" , method: .post, headers: nil ,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { [weak self] response in
guard self != nil else {
return
}
debugPrint(response)

}

upload.responseJSON { response in

print(response)
// let responseJSON = response.result.value as! NSDictionary


// print(responseJSON)



}

case .failure(let encodingError):
print("error:\(encodingError)")
}


})
tableview.reloadData()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableview.reloadData()
}

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return myarray.count
return arrRes.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell") as! CategoryCellTableViewCell
var dict = arrRes[indexPath.row]

cell.catName.text = dict["category.NAME"] as? String
cell.total?.text = dict["TOTAL"] as? String


return cell
}

和我的 CategoryCellTableViewCell.swift 代码:

import UIKit

class CategoryCellTableViewCell: UITableViewCell {

@IBOutlet weak var catName: UILabel!

@IBOutlet weak var total: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

我试图在网上寻求一些帮助,但无法获得,但非常感谢您花时间:)

字典输出:

SUCCESS: (
{
NAME = "COSMETIC PRODUCTS & SERVICES";
TOTAL = 1;
id = 54300;
},
{
NAME = TATTOOING;
TOTAL = 1;
id = 225700;
}

)

最佳答案

试试这个我想你忘记了填充数组,还设置了数据源,以及 UITableView 的委托(delegate)

import UIKit
import Alamofire
import SwiftyJSON


class CategoryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSURLConnectionDelegate {

var tableData = Array<Category>()
var category = [Category]()
var arrRes = [NSDictionary]()
var catTitle = ""
var id = ""



@IBOutlet weak var catTitleRec: UILabel!

@IBOutlet weak var tableview: UITableView!




override func viewDidLoad() {
super.viewDidLoad()

catTitleRec.text = catTitle
loadCategories(id:id)
}

func loadCategories(id : String) {


let userDic : [String : AnyObject] = ["id":id as AnyObject]

Alamofire.upload(multipartFormData: { multipartFormData in

for (key, value) in userDic {
multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
}

},to: "URL" , method: .post, headers: nil ,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { [weak self] response in
guard self != nil else {
return
}
debugPrint(response)
}
upload.responseJSON { response in
print(response)

self.arrRes = response.result.value as? NSArray as! [NSDictionary]
DispatchQueue.main.async {
self.tableview.reloadData()
}
}

case .failure(let encodingError):
print("error:\(encodingError)")
}


})
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableview.reloadData()
}

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return myarray.count
return arrRes.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell") as! CategoryCellTableViewCell
var dict = arrRes[indexPath.row]
cell.catName.text = dict["NAME"] as? String
cell.total?.text = dict["TOTAL"] as? String


return cell
}



}

关于ios - swift 3/Alamofire : Cant load the table with the data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838857/

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