user5513630
user5513630

Reputation: 1699

how to get count of titles in my json data , which are display in my table view

I have one table view which contains some title and sub title. And in my table view it will show the title and subtitle form my json data.

It also have one label called resultcount which will display the count of title in that label. But now its showing 0, though my data is showing in table view.

Here my code:

var arrDict :NSMutableArray=[]
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let nib = UINib(nibName:"customCell", bundle: nil)

    tableView.registerNib(nib, forCellReuseIdentifier: "cell")
    Resultcount.text = "\(arrDict.count) Results"
    self.jsonParsingFromURL()
}

func jsonParsingFromURL () {
    let url = NSURL(string: "url")
    let session = NSURLSession.sharedSession()

    let request = NSURLRequest(URL: url!)
    let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
        print("done, error: \(error)")

        if error == nil
        {
            dispatch_async(dispatch_get_main_queue()){
                self.arrDict=(try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray

                print(self.arrDict)
                if (self.arrDict.count>0)
                {
                    self.tableView.reloadData()
                }
                // arrDict.addObject((dict.valueForKey("xxxx")
            }


        }
        dataTask.resume()


    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell


        cell.vendorName.text = tableData[indexPath.section]
        cell.vendorAddress.text = tableAdd[indexPath.section]





        cell.vendorName.text=arrDict[indexPath.row] .valueForKey("name")   as? String
        cell.vendorAddress.text=arrDict[indexPath.row] .valueForKey("address")   as? String



        return cell


    }

}

my `resultCount` label code is :
Resultcount.text = "\(arrDict.count) Results"

But its showing 0.

Upvotes: 0

Views: 88

Answers (1)

Muzahid
Muzahid

Reputation: 5186

When your view loaded then you set Resultcount.text = "\(arrDict.count) Results". But your data still not downloaded.

But when your download completed then you did not update the label text. Thats why updated value does not show. So update the label text when download completed.

Put this Resultcount.text = "\(arrDict.count) Results" under the dispatch_async(dispatch_get_main_queue()) in your jsonParsingFromUR method.

func jsonParsingFromURL () {
    let url = NSURL(string: "some url")
    let session = NSURLSession.sharedSession()
    let request = NSURLRequest(URL: url!)
    let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

      print("done, error: \(error)")

      if error == nil{
        dispatch_async(dispatch_get_main_queue()){
          self.arrDict=(try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray

          print(self.arrDict)

          if (self.arrDict.count>0){
            Resultcount.text = "\(arrDict.count) Results"
            self.tableView.reloadData()
          }

        }

        // arrDict.addObject((dict.valueForKey("xxxx")
      }

    }
    dataTask.resume()
  }

Upvotes: 1

Related Questions