I have a timer in which it calls the updateInterval every second. This timer will calculate the time remaining and put the result as a NSString. In other words the NSString is changing every second and from what I did below, the UITable is then reloaded every 1 second. The issue is that it crashes at countdown.text = labelText;
// inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//blah
else if (indexPath.row == 1 && indexPath.section == 0){
UILabel * countdown = [[UILabel alloc] init];
countdown.text = labelText;
[cell.contentView addSubview:countdown];
cell.textLabel.text = @"Time remaining";
}
//blah
- (void)updateInterval:(NSTimer *)timer {
NSTimeInterval timeinterval = [(NSDate *)[data valueForKey:@"start_date"] timeIntervalSinceDate开发者_如何学C:[NSDate date]];
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:timeinterval sinceDate:date1];
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
labelText = [NSString stringWithFormat:@"%d day %d:%d:%d", [breakdownInfo day], [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]];
//NSLog(@"%d day %d:%d:%d", [breakdownInfo day], [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]);
[self.table reloadData];
[date1 release];
[date2 release];
}
Why is this?
If you are getting bad access in console then it may be because you have to retain labelText in your updateInterval method.
精彩评论