我在视图类中有一个全局变量,我在viewDidLoad()中执行一些函数,并通过这些函数为这些全局变量(数组)分配一些值。但是当我尝试在viewDidLoad()之外重用它们时,我得到它们的count = 0就像这个函数一样:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return prodItem.count;
}
这是我的完整代码:
import UIKit
class NewViewController: UIViewController, UITableViewDelegate {
//define product details vars
var prodLink = [String]();
var prodImg = [String]();
var prodItem = [String]();
var prodDesc = [String]();
var prodPrice = [String]();
var prodSale = [String]();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//define url
let url = NSURL(string: "https://www.iravin.com/new")!;
//define task
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) -> Void in
if let urlContent = data {
let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding);
let webArray = webContent?.componentsSeparatedByString("<ul class=\"hProductItems clearfix\">");
let webArray2 = webArray?[1].componentsSeparatedByString("</ul>");
var prodArray = webArray2?[0].componentsSeparatedByString("<li class=\"span3 clearfix\">");
prodArray?.removeAtIndex(0);
for oneProd in (prodArray)! {
var prodData = [String]();
prodData = oneProd.componentsSeparatedByString("\n\t\t\t\t\t\t\t<div class=\"thumbnail\">\n\t\t\t\t\t\t\t\t<a href=\"");
prodData.removeAtIndex(0);
prodData = prodData[0].componentsSeparatedByString("\"><img class=\"lazy\" data-src=\"");
self.prodLink.append(prodData[0]);
prodData = prodData[1].componentsSeparatedByString("\" src=\"img/loading.gif\" alt=\"\"></a>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t<div class=\"thumbSetting\">\n\t\t\t\t\t\t\t\t<div class=\"thumbTitle\">\n\t\t\t\t\t\t\t\t\t<h3>\n\t\t\t\t\t\t\t\t\t<span class=\"invarseColor\">");
self.prodImg.append(prodData[0]);
prodData = prodData[1].componentsSeparatedByString("</span>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</h3>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t<div class=\"product-desc\">\n\t\t\t\t\t\t\t\t\t<p>");
if prodData[0].containsString("Sale") {
let prodDataSale = prodData[0].componentsSeparatedByString("</span>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"label label-info\">Sale");
self.prodItem.append(prodDataSale[0]);
prodData = prodData[1].componentsSeparatedByString("</p>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t<div class=\"thumbPrice\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span>");
self.prodDesc.append(prodData[0]);
prodData = prodData[1].componentsSeparatedByString("</span>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</div>\n\n\t\t\t\t\t\t\t\t<div class=\"thumbButtons\">\n\t\t\t\t\t\t\t\t\t<button rel=\"");
var prodDataPrice = prodData[0].componentsSeparatedByString("<span class=\"strike-through\">");
prodDataPrice = prodDataPrice[1].componentsSeparatedByString("</span>");
self.prodPrice.append(prodDataPrice[0]);
self.prodSale.append(prodDataPrice[1]);
} else {
self.prodItem.append(prodData[0]);
prodData = prodData[1].componentsSeparatedByString("</p>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t<div class=\"thumbPrice\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span>");
self.prodDesc.append(prodData[0]);
prodData = prodData[1].componentsSeparatedByString("</span>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</div>\n\n\t\t\t\t\t\t\t\t<div class=\"thumbButtons\">\n\t\t\t\t\t\t\t\t\t<button rel=\"");
self.prodPrice.append(prodData[0]);
self.prodSale.append(prodData[0]);
}
}
}
}
task.resume();
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return prodItem.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// get an instance of your cell
let cell = tableView.dequeueReusableCellWithIdentifier("prodCell", forIndexPath: indexPath) as! MyCustomTableViewCell;
cell.lblProdItem.text = self.prodItem[indexPath.row];
return cell;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
来自ios dev库:与大多数网络API一样,NSURLSession API是高度异步的。
您需要在任务完成后重新加载tableview。现在它开始任务,然后在完成之前查看计数,这就是为什么它是0。
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
task.resume();
}