ios - Swift | UICollectionView without storyboard shows incorrect data -


i building app has uicollectionview 3 items. each item takes complete width of screen , shows timer counts down next departure times our busses in each location.

each item contains 1) departure address, 2) string "next shuttle leaves @ time", , 3) timer counts down next departure time in 00:00:00 format.

item 1 , 2 display expected, when item 3 departure address , string correct, timer shows time cell 1 instead of cell 3.

i setting uicollectionview like:

func setupcollectionview() {         let layout: uicollectionviewflowlayout = uicollectionviewflowlayout()     layout.sectioninset = uiedgeinsets(top: 0, left: 0, bottom: 0, right: 0)     layout.itemsize = frame.size     layout.minimumlinespacing = 0     layout.minimuminteritemspacing = 0     layout.scrolldirection = uicollectionviewscrolldirection.horizontal     let collframe:cgrect = cgrectmake(0, 0, self.frame.width, self.frame.height)     collectionview = uicollectionview(frame: collframe, collectionviewlayout: layout)     collectionview!.datasource = self     collectionview!.delegate = self     collectionview!.registerclass(nextshuttlecell.self, forcellwithreuseidentifier: "cell")     collectionview!.backgroundcolor = uicolor.whitecolor()     collectionview!.pagingenabled = true     collectionview!.scrollindicatorinsets = uiedgeinsetszero     collectionview!.layer.borderwidth = 0     collectionview!.layer.bordercolor = uicolor.clearcolor().cgcolor     self.addsubview(collectionview!)  } 

and creating items using:

func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {      let cell:nextshuttlecell = collectionview.dequeuereusablecellwithreuseidentifier("cell", forindexpath: indexpath) as! nextshuttlecell      switch(indexpath.row) {     case 0:         cell.setcellcolor(help.darkaccent1())         cell.setlocationlabel("auckland, victoria street")         cell.schedule = getschedulefor("victoria street")     case 1:         cell.setcellcolor(help.darkaccent2())         cell.setlocationlabel("wellington, airedale street")         cell.schedule = getschedulefor("airedale street")     case 2:         cell.setcellcolor(help.darkaccent3())         cell.setlocationlabel("airport, auckland")         cell.schedule = getschedulefor("auckland airport")     default: break     }      return cell  } 

when view instantiated retrieves schedule parse.com. getschedulefor function iterates through schedule , returns relevant locations schedule object custom cell.

i know getschedulefor function working correctly pulls right information first 2 items, , if change order of items (e.g. swap item 3 code item 1, correct data shows in item 1 item 3 incorrect.

also note timer incorrect on item 3, remaining information correct , pulled schedule object - know it's pulling right data.

i moved uitableview , showed correct data seems way uicollectionview manages it's items.

edit:

i using custom uicollectionviewcell , initialising using:

override init(frame: cgrect) { super.init(frame: frame)     inittimer() } 

this gets next shuttle time , initialises timer update count down label.

func inittimer() {             nextshuttletime = getnextshuttletime()     timetillnextshuttle = nstimer.scheduledtimerwithtimeinterval(0.1, target: self, selector: "updatetime", userinfo: nil, repeats: true) } 

then, gets time using following:

func updatetime() {      let nextshuttleat:nstimeinterval = nstimeinterval(nextshuttletime.timeintervalsincenow)     let currenttime:nstimeinterval = nstimeinterval(nsdate().timeintervalsincenow)     let timeleft:nstimeinterval = nextshuttleat - currenttime      if timeleft >= 0 {         let seconds = timeleft         timelabel.text = "\(stringfromtimeinterval(seconds))"         self.updatenextshuttlelabel()     } else {         timetillnextshuttle.invalidate()         inittimer()     }  } 

working solution:

as per comments, stopped getnextshuttletime() being assigned variable , instead called in updatetimer() function. updated per below:

func inittimer() {             timetillnextshuttle = nstimer.scheduledtimerwithtimeinterval(0.1, target: self, selector: "updatetime", userinfo: nil, repeats: true) }  func updatetime() {      let nextshuttleat:nstimeinterval = nstimeinterval(getnextshuttletime().timeintervalsincenow)     let currenttime:nstimeinterval = nstimeinterval(nsdate().timeintervalsincenow)     let timeleft:nstimeinterval = nextshuttleat - currenttime      if timeleft >= 0 {         let seconds = timeleft         timelabel.text = "\(stringfromtimeinterval(seconds))"         self.updatenextshuttlelabel()     } else {         timetillnextshuttle.invalidate()         inittimer()     }  } 

your nextshuttlecell objects re-used, need make sure updated when cell assigned - can't rely on init() called first time cell assigned.

rather storing nextshuttletime variable in inittimer() can call getnextshuttletime() in updatetime - way correct time.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -