UIImage not appearing in prototype cells in UITableView (Swift) -
i have created uitableview containing 10 cells, each of have uiimageview attached them using auto-layout
the table populated in backtableviewcontroller code:
tablearray = ["home page", "membership card", "recent news", "fitness schedule", "training log", "pool lap calculator", "martial arts schedule", "pool schedule", "comments", "acknowledgements"]
the issue none of these images appear in table until cell selected while run. out of ideas why is... anyone?
thanks
edit: backtableviewcontroller
import uikit
class backtableviewcontroller: uitableviewcontroller {
var tablearray = [string]() var imagearray = [string]() override func viewdidload() { super.viewdidload() tablearray = ["home page", "membership card", "recent news", "fitness schedule", "training log", "pool lap calculator", "martial arts schedule", "pool schedule", "comments", "acknowledgements"] imagearray = ["home-50.png","credit_card-50.png","info-50.png","math-50.png","news-50.png","report_card-50.png","weightlift-50.png"] // additional setup after loading view. } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return tablearray.count } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier(tablearray[indexpath.row], forindexpath: indexpath) as! uitableviewcell cell.textlabel?.text = tablearray[indexpath.row] return cell } }
you need one prototype cell reuse identifier. clue in name "prototype". think of them blueprints cells based off.
in view controller class (which should uitableviewdelegate
if uitableviewcontoller
) specify number of rows method:
override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return yourarray.count }
this method takes care of displaying right info in right row of table.
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { //we create cell our prototype gave identifier in our storyborad. let cell = tableview.dequeuereusablecellwithidentifier("itemcell") as! uitableviewcell //create single instance of item our items array. use indexpath.row passed method index array. let item = yourarray[indexpath.row] //you can go array , right info out regarding row using indexpath.row cell.textlabel?.text = item cell.imageview?.image = uiimage(named: item) //finally method returns correctly configured cell fro tableview display. return cell }
hope helps.
Comments
Post a Comment