I have some table cells displaying different time. I want the time to be updated (just like countdown).
I tried to call [my_table_name reloadData] every second but the method does not invoke logics in cellForRowAtIndexPath. How can I loop through the cells of UITableView to update its contents?
UPDATE: the time displayed is in form of 10 days 11:12:49 ( each cell has an expiry date, when the table is updated, the expiry date subtracts current time & calculate the time remaining , which is displayed on cell )
UPDATE 2: There are 2 methods to reload data. One is calling from server, one is calling the function only.
- (void)reloadLocal {
[my_table_name reloadData];
}
and the online method is:
- (void)reloadFromServer {
[self parseXML:@"URL here"];
}
- (void)parseXML:(NSString *)URL {
// read the XML & update a NSArray containing the data
}
the reloadLocal method is called like this (in viewDidLoad )
current = [[NSDate date] retain];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reloadLocal) userInfo:nil repeats:YES];
for the cellForRowAtIndexPath method, here is the pseudo codes (too long to post all lines):
static NSString *identifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:identifier owner:nil options:nil];
for (id currendObj in topLevelObjects) {
if ([currendObj isKindOfClass:[UITableViewCell class]]) {
cell = (CustomCell *)currendObj;
break;
}
}
// then insert data to related UIOutlet elements.
NSDate *validUntil = [[NSDate alloc] initWithTimeInterval:diff sinceDate:current];
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:current toDate:validUntil options:0];
cell.lbl_day.text = [NSString stringWithFormat:@"%d", [conversionInfo day]];
cell.lbl_minute.text = [NSString stringWithFormat:@"%d", [conversionInfo minute]];
cell.lbl_second.text = [NSString stringWithFormat:@"%d", [conversionInfo second]];
cell.lbl_hour.text = [NSString stringWithFormat:@"%d", [conversionInfo hour]];
note that current is an instance of current time, in data type NSDate.
End up I discovered if I update the date variable
currentinreloadLocal, the time displaying will automatically be updated.