简体   繁体   中英

Use global variables outside viewDidLoad() function

I have a global variables within view class, I am doing some functions inside viewDidLoad(), and through those functions I assign some values to those global variables (arrays). But when I try to reuse them outside viewDidLoad() I get their count = 0 like this function:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

     return prodItem.count;

}

Here's my full code:

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.
}

}

From ios dev library: Like most networking APIs, the NSURLSession API is highly asynchronous.

You need to reload your tableview after the task has been completed. Right now its starting the task and then looking at the count before it has finished which is why it is 0.

            }
           dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
           }
        }
    }
    task.resume();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM