0

In my app I can add an event and they event will show up in a tableView as cell with the information of the event. Now I want to be able to set the date of the event and then the cell will only add to the tableView if that date is before the current date (the event happened in the past). Heres the code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let e = events[indexPath.row]
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
    let Date1 = e.eventDate
    let Date2 = NSDate()

    if Date1!.earlierDate(Date2).isEqualToDate(Date1!) {

        // Sets labels in cell to whatever user typed in on the events page
        cell.titlePrototypeCell!.text = e.eventTitle!

    } else {

        //Code to not add a cell

    }

    return cell

}

The problem with this is that if the event date is after the current date (in the future) then a regular cell is added to the tableView instead of no cell at all. Any ideas on how to not add a cell if the event is in the future?

  • same logic you used in number of rows delegate method ?. Debug and check . Get the time in milliseconds and compare the values. Then check. – Rince Thomas Jun 27 '16 at 05:59
  • you have to filters your data first – Bagus Cahyono Jun 27 '16 at 06:03
  • For date comparison, refer this SO Qn http://stackoverflow.com/questions/26198526/nsdate-comparison-using-swift – Rince Thomas Jun 27 '16 at 06:03
  • I think that the if statement in the code works it just that Im not sure how to not add a cell if the date is in the future, I edited the code, maybe the problem makes more sense – Thor Larson Jun 27 '16 at 06:04

1 Answers1

0

You have to do one thing. 1) Remove object from the events array which is not contain a future date.

2) Reload your table data.

So you can get a record with the future date only

This code might be help you. Call this method just before the get your response.

func RemoveData(){
     for var i = events.count-1; i >= 0 ; i-- {
      let e = events[i]
      let Date1 = e.eventDate
      let Date2 = NSDate()
      if Date1!.earlierDate(Date2).isEqualToDate(Date1!) == false{
            events.removeObjectAtindex(i)
      }
    }
}
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65