gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:35:27 25 4
gpt4 key购买 nike

当我从 url 下载信息后,我试图填充一个表。此数据将填充不同的标签和嵌套在 View Controller 中的 TableView 中的 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)
}

}

最佳答案

下载完所有数据后,在 TableView 上调用 reloadData

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com