ios - Populate Table View With Object Properties? -


i have created custom class within uitableviewcontroller contain 2 properties :

import uikit  class firsttableviewcontroller: uitableviewcontroller {  class continent {      var name: string = "country name"     var continentcountries: nsarray = ["countries of selected continent"] } 

now i've created several objects , placed them in array:

func setup() {  var asia = continent()     asia.name = "asia"     asia.continentcountries = ["afghanistan","armenia","azerbaijan","bahrain"] //etc..  var africa = continent() africa.name = "africa" africa.continentcountries = ["egypt","sierra leone","sudan","south africa"] //etc..  var northamerica - continent()  northamerica.name = "north america" northamerica.continentcountries = ["canada","united states of america","mexico"]   let theworld = [asia,africa,northamerica]   } 

notice had declare newly created objects , array in " func setup() " method because not recognized "continent" object "firsttableviewcontroller" object

now goal populate following table view first property of each object being "name", , used following code:

let acontinent = theworld[indexpath.row] as! continent cell.textlabel.text = acontinent.name 

now issue is, outside of setup() method, controller not recognize, , labels "undeclared", there no way can add code of protocol delegates of tableviewcontroller being :

override func numberofsectionsintableview(tableview: uitableview) -> int {  return 1 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {  return 3 //this temporary int }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("cell") as! uitableviewcell      return cell 

what should corrected? how allow newly created class , objects act in unison initial uitableviewcontroller class can populate table view?

declare property this,so can use in delegate method

class firsttableviewcontroller: uitableviewcontroller {  class continent {      var name: string = "country name"     var continentcountries: nsarray = ["countries of selected continent"] } let theworld:[continent] = {     var asia = continent()     asia.name = "asia"     asia.continentcountries = ["afghanistan","armenia","azerbaijan","bahrain"] //etc..      var africa = continent()     africa.name = "africa"     africa.continentcountries = ["egypt","sierra leone","sudan","south africa"] //etc..      var northamerica = continent()     northamerica.name = "north america"     northamerica.continentcountries = ["canada","united states of america","mexico"]       return  [asia,africa,northamerica]     }()  override func numberofsectionsintableview(tableview: uitableview) -> int {      return 1 }  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {      return theworld.count }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell     cell.textlabel?.text = theworld[indexpath.row].name     return cell } } 

screenshot

enter image description here


Comments