Answer the question
In order to leave comments, you need to log in
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
}
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
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question