ios - 从 url 下载数据时等待填充表格 View

标签 ios arrays json swift uitableview

当我从网址下载信息后,我试图填充表格。此数据将填充嵌套在 View Controller 中的表格 View 中的不同标签和 ImageView 。我从本地 json 文件获取数据以正确解析,并用正确的值填充标签。问题是填充表的表方法在我可以从 url 下载数据之前被调用。对此的任何帮助将不胜感激。 谢谢

这是我到目前为止所拥有的:

var titleArray = [String]()
var descriptionArray = [String]()
var amountArray = [Int]()
var typeArray = [String]()
var startDateArray = [String]()
var endDateArray = [String]()
var barcodeArray = [String]()


@IBOutlet weak var myTableView: UITableView!
// MARK: - UIViewController lifecycle

override func viewDidLoad() {
    super.viewDidLoad()
    myTableView.dataSource = self
    myTableView.delegate = self
    downloadCouponData(couponUrl)


}

// MARK: - UITableViewDataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print("Running Table view that counts the number of rows")
    return titleArray.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Coupon", forIndexPath: indexPath) as! CouponTableViewCell

    print("Running TableView that fills the table")

    var amountText : String
    amountText = String( amountArray[indexPath.row])

    var typeType : String = ""

    if(typeArray[indexPath.row] == "PERCENT_OFF")
    {
        typeType = "%"
    }else
    {
        typeType = "¥ off"
    }

    cell.couponTitle.text = titleArray[indexPath.row]
    cell.couponDescription.text = descriptionArray[indexPath.row]
    cell.couponAmount.text = amountText + typeType
    cell.couponStartDate.text = startDateArray[indexPath.row]
    cell.couponEndDate.text = endDateArray[indexPath.row]
    cell.couponBarcodeNumber.text = barcodeArray[indexPath.row]
    let img = Barcode.fromString(barcodeArray[indexPath.row])
    cell.couponBarcode.image = img


    return cell
}

class Barcode {
    class func fromString(string : String) -> UIImage? {
        let data = string.dataUsingEncoding(NSASCIIStringEncoding)
        let filter = CIFilter(name: "CICode128BarcodeGenerator")
        filter!.setValue(data, forKey: "inputMessage")
        return UIImage(CIImage: filter!.outputImage!)
    }
}

//Function to log into the server and retrive data
func  downloadCouponData(myUrl : String)
{
    print("Downloading Coupon Data")
    Alamofire.request(.GET, myUrl)
        .authenticate(user: "admin", password: "admin")
        .validate()
        .responseString { response in
            print("Success: \(response.result.isSuccess)")
            self.parseCoupons(response.result.value!)

    }
}

func parseCoupons(response : String)
{
    print("Starting to parse the file")
    let data = response.dataUsingEncoding(NSUTF8StringEncoding)
    var myJson : NSArray
    myJson = []

    do {
        myJson = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSArray
        print("MyJson lenght" , myJson.count)
    }
    catch {
        print("Error")
    }


    for i in 0..<myJson.count
    {
        titleArray.append((myJson[i]as! NSDictionary)["name"] as! String)
        descriptionArray.append((myJson[i]as! NSDictionary)["description"] as! String)
        amountArray.append((myJson[i]as! NSDictionary)["amount"] as! Int)
        typeArray.append((myJson[i]as! NSDictionary)["type"] as! String)
        startDateArray.append((myJson[i]as! NSDictionary)["start_date"] as! String)
        endDateArray.append((myJson[i]as! NSDictionary)["end_date"] as! String)
        barcodeArray.append((myJson[i]as! NSDictionary)["barcode"] as! String)
    }

    for gus in descriptionArray{
        print("descr array: " + gus)
    }
    for gus in amountArray{
        print("Amount array: " , gus)
    }
    for gus in typeArray{
        print("Type array: " + gus)
    }
    for gus in startDateArray{
        print("Start array: " + gus)
    }
    for gus in endDateArray{
        print("End array: " + gus)
    }
    for gus in barcodeArray{
        print("Bar array: " + gus)
    }

    for gus in titleArray{
        print("Title array: " + gus)
    }

}

}

最佳答案

下载完所有数据后,在表格 View 上调用 reloadData

关于ios - 从 url 下载数据时等待填充表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38298249/

相关文章:

ios - 如果我在 XIB 中设计了 textField,如何以编程方式更改 UItextField 的 placeHolder 文本

c - C 中奇怪的内存问题

json - 如何在 swift 中使用 utf-8 解码 json 文件 3/4

java - 使用正则表达式将字符串数据的键和值格式化为 json 键值对

iOS : device is hanging while running an intensive computing task

iphone - 应用程序可移植性(在 iPhone 和 iPad 上)

ios - 使用 NSNumberFormatter 生成小数点后带有尾随零的数字

arrays - Swift - 更新嵌套字典中特定项目的值

php - Cakephp 3+ 将数组保存在mysql的一个字段中

json - 从 R 中的 json 文件导入数据