ios - Best way of working with ListView -


hey guys i´m in process of learning swift right , try program game. want show list items , different attributes these items. first have user choice of can select either food or toys or other stuff coming in future. here tried 1 viewcontroller , change stuff inside depending on choice. right have these items in array class. this:

    class fooddata {     var name: string     var description = "basic food"     var owned = 0     var taste = 0     var price = 0     var water = 0     var image = "default.png"     init(name: string){     self.name=name     } } class toydata {     var name: string     var description = "basic item"     var owned = 0     var price = 0     var joy = 0     var image = "default.png"     init(name: string){     self.name=name     } } 

i initialise these with:

    var foodload=[fooddata]()       func loadfooddata(){     foodload.append(fooddata(name: "icecream"))     foodload[0].description="very cold"     foodload[0].owned=10 } 

same style toys. have these 2 classes in 2 arrays called foodload[i] , toyload[i]

for table view fill protocols

func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let shopcell = tableview.dequeuereusablecell(withidentifier: shopcellidentifier, for: indexpath) as! shopcellstyle      shopcell.shopnamelabel?.text = shopdata[indexpath.row].name     shopcell.shopimageview?.image = uiimage(named: shopdata[indexpath.row].image)     shopcell.shoppricelabel?.text = string(shopdata[indexpath.row].price) + currency     return shopcell 

so idea assign shopdata user choice. if assign shopdata = foodload, can't change toyload anymore. maybe can give me hint of how solve best way.

for cellforrowat indexpath:

override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {     let shopcell = tableview.dequeuereusablecell(withidentifier: "shopcellidentifier", for: indexpath) as! itemcell      if selection == "food" {         shopcell.shopnamelabel?.text = foodload[indexpath.row].name         shopcell.shopimageview?.image = uiimage(named:          foodload[indexpath.row].image)         shopcell.shoppricelabel?.text = string(foodload[indexpath.row].price) + currency     }      else if selection == "toys" {         shopcell.shopnamelabel?.text = toyload[indexpath.row].name         shopcell.shopimageview?.image = uiimage(named:          toyload[indexpath.row].image)         shopcell.shoppricelabel?.text = string(toyload[indexpath.row].price) + currency    }    return shopcell } 

you'd want numberofrowsinsection uitableview function. call tableview.reloaddata() when user changes type selection.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -