【问题标题】:Waiting to fill in a tableview when you data is downloaded from a url从 url 下载数据时等待填写表格视图
【发布时间】:2016-11-12 21:17:58
【问题描述】:

当我从 url 下载信息后,我试图填充表格。这些数据将填充不同的标签和嵌套在视图控制器中的表格视图中的图像视图。我从本地 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)
    }

}

}

【问题讨论】:

  • 在得到响应并完成数据解析后重新加载表。只是 self.myTableView.reloadData()

标签: ios arrays json swift uitableview


【解决方案1】:

下载所有数据后,请在表格视图上致电 reloadData

【讨论】:

  • 谢谢你修复它。我查看了很多教程,但无法找到如何做到这一点。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 2017-10-26
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
相关资源
最近更新 更多