ios - To change the background color of the cells with the earlier date -
each cell contains date , information(text). default, sorting in reverse order. sorted in reverse order of time. want change background color of cells in cell prior current date.
tableview cellforrowatindexpath :
let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! tableviewcell cell.datelabel.text = stores.date cell.contentlabel.text = stores.content let today = nsdate() if today == (stores.date) { cell.backgroundcolor = uicolor.bluecolor() } else { cell.backgroundcolor = uicolor.clearcolor() } return cell
your date comparison wrong. use nscalendar compare nsdate day. nsdate extension described here getting difference between 2 nsdates in (months/days/hours/minutes/seconds)
extension nsdate { // ... func daysfrom(date:nsdate) -> int{ return nscalendar.currentcalendar().components(.day, fromdate: date, todate: self, options: []).day } //... }
using extension in code:
let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! tableviewcell cell.datelabel.text = stores.date cell.contentlabel.text = stores.content if (stores.date.daysfrom(nsdate()) == 0) { cell.backgroundcolor = uicolor.bluecolor() } else { cell.backgroundcolor = uicolor.clearcolor() } return cell
Comments
Post a Comment