更新 UITableCell 中的计时器值
我有一个计时器,它每秒调用 updateInterval 。该计时器将计算剩余时间并将结果作为 NSString。换句话说,NSString 每秒都在变化,从我下面所做的来看,UITable 然后每 1 秒重新加载一次。问题是它在 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:[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];
}
这是为什么?
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:[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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在控制台中访问不良,则可能是因为您必须在 updateInterval 方法中保留 labelText。
If you are getting bad access in console then it may be because you have to retain labelText in your updateInterval method.