MNM MNM - 4 months ago 43
JSON Question

Swift: waiting to fill in a tableview when you data is downloaded from a url

i am trying to get a table filled when after I downloaded information from a url.This data will fill in different labels and a image view in the table view which is nested in a view controller. I got the data from a local json file to parse out right and also fill in the labels with the right values. The problem is the table methods that fill the table get called before I can download the data from the url. Any help with this will be appreciated.

Here is what I have so far:

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() {
myTableView.dataSource = self
myTableView.delegate = self


// 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 = "%"
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")
.responseString { response in
print("Success: \(response.result.isSuccess)")


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 {

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)




Call reloadData on the table view once you have downloaded all your data.