Swift 3 / Alamofire:无法使用数据加载表 [英] Swift 3 / Alamofire: Cant load the table with the data

查看:76
本文介绍了Swift 3 / Alamofire:无法使用数据加载表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to load the data on the UItableview, I am able to see the records fetched in the output panel but not able to fetch them on the 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代码:

and my CategoryCellTableViewCell.swift code:

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
}

}

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

I tried to seek some help online but couldnt get but thanks a lot for the time :)

dict输出:

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

推荐答案

试试这个
我想您忘记了填充数组,还设置了数据源和UITableView的委托

Try this I think you forget the filling array and also set the datasource,and delegate of UITableView

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 
    } 



}

这篇关于Swift 3 / Alamofire:无法使用数据加载表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆