我在从网址下载信息后尝试填充表格。此数据将在表格视图中填充不同的标签和图像视图,该视图嵌套在视图控制器中。我从本地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)
}
}
}
答案 0 :(得分:2)
下载完所有数据后,在表格视图中调用reloadData
。