uitableview - Not able to access the variable declared outside the function in UITableviewcontroller swift -


i using simple uitableviewcontroller

var testdata = [string]()  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("controlcell", forindexpath: indexpath) as! tableviewcell      // configure cell...     cell.clabel.text = testdata[indexpath.row]     return cell } 

testdata declared outside function. testdata populated method.

the issue is: self.testdata shows no data when put breakpoint , check data. using variable correctly in swift ?

adding complete code here:

 import uikit  import alamofire    class tableviewcontroller: uitableviewcontroller, uitableviewdatasource,             uitableviewdelegate {  struct listitem {     var name:string = ""     var number:string = ""     var email:string = "" }   override func viewdidload() {     super.viewdidload()     getjsondata()      // uncomment following line preserve selection between presentations     // self.clearsselectiononviewwillappear = false      // uncomment following line display edit button in navigation bar view controller.     // self.navigationitem.rightbarbuttonitem = self.editbuttonitem() }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  // mark: - table view data source  override func numberofsectionsintableview(tableview: uitableview) -> int {     // #warning potentially incomplete method implementation.     // return number of sections.     return 1 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     // #warning incomplete method implementation.     // return number of rows in section.    // return controlstaticdata.count    // return controllistdata.controllistitems.count     return testdata.count }     var testdata = [string]()       func getjsondata(){       var jsondata:nsdata!      let url = "http://localhost:3000/controllistapp"      alamofire.request(.get, url).responsejson(){         (_,_,data,_) in         println(data)      // parse jsondata returned alamofire .get request      let json = json(data!)     var = 0     for(_, subjson): (string, json) in json {         if let name = subjson["name"].string,             let number = subjson["number"].string,             let email = subjson["email"].string{                 var item = listitem(name: name, number: number, email: email)                 self.testdata += [name]          }      } }  }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("controlcell", forindexpath: indexpath) as! tableviewcell      // configure cell...     cell.clabel.text = self.testdata[indexpath.row]     return cell }  } 

a breakpoint @ line self.testdata += [name] in getjsondata shows data being populated. however, breakpoint @ line cell.clabel.text = self.testdata[indexpath.row] in tableview function shows there no data , throws exception.

i think problem forgot include self.tableview.reloaddata() after populate data array


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -