A
A
Anton Parfenov2015-12-23 13:16:18
iOS
Anton Parfenov, 2015-12-23 13:16:18

The variable does not change, does not receive data, or something else?

Guys, here is a piece of code:

var nubmerOfRows = 0
    var namesArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var titleView : UIImageView
        titleView = UIImageView(frame:CGRectMake(0, 0, 10, 30))
        titleView.contentMode = .ScaleAspectFit
        titleView.image = UIImage(named: "title_logo")
        
        self.Navigator.titleView = titleView
        
        parseJSON(url)
        
    }
    
    func parseJSON (urlToRequest: String) {
        let request = HTTPTask()
        var attempted = false
        request.auth = {(challenge: NSURLAuthenticationChallenge) in
            if !attempted {
                attempted = true
                return NSURLCredential(user: self.usr, password: self.pswd, persistence: .ForSession)
            }
            return nil //auth failed, nil causes the request to be properly cancelled.
        }
        request.GET(urlToRequest, parameters: nil, completionHandler: {(response: HTTPResponse) in
            if let err = response.error {
                NSLog("error: \(err.localizedDescription)")
                return //also notify app of failure as needed
            }
            if let data = response.responseObject as? NSData {
                let readableJSON = JSON(data: data, options: NSJSONReadingOptions.MutableContainers, error: nil)
                
                self.nubmerOfRows = readableJSON["result"].count - 1
                
                if (self.nubmerOfRows > 0) {
                    for i in 0...self.nubmerOfRows {
                        let cat_name = readableJSON["result"][i]["name"].string!
                        self.namesArray.append(cat_name)
                    }
                }
            }
        })
        
    }
    
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nubmerOfRows
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        if namesArray.count != 0 {
            cell.textLabel?.text = namesArray[indexPath.row]
        }
        return cell
    }

Why does namesArray receive data inside the function, but nothing is left in it externally? The code works well, it goes to the site, authorization, getting json, parsing it and collecting this variable in the form of an array in a loop. But there are no rows in the table on the iPhone and the variable only collects rows in this place:
if (self.nubmerOfRows > 0) {
                    for i in 0...self.nubmerOfRows {
                        let cat_name = readableJSON["result"][i]["name"].string!
                        self.namesArray.append(cat_name)
                    }
                }

And here is this part:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nubmerOfRows
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        if namesArray.count != 0 {
            cell.textLabel?.text = namesArray[indexPath.row]
        }
        return cell
    }

doesn't work because empty nubmerOfRows and namesArray.
Who will tell you what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ifau, 2015-12-23
@goldbest

After the for loop do

dispatch_async(dispatch_get_main_queue()) {
  self.tableView.reloadData()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question