Ios 如何更新UITableViewCell内标签中的运行计时器
我想在tableview中用户点击的任何单元格内的标签内设置运行时间或持续时间计时器。数据源和委托是承载ViewController的对象 我将标签拖到tableview中的prototype单元格中,然后使用以下代码驱动标签中的文本: 首先,tableview方法:Ios 如何更新UITableViewCell内标签中的运行计时器,ios,uitableview,uilabel,nstimer,Ios,Uitableview,Uilabel,Nstimer,我想在tableview中用户点击的任何单元格内的标签内设置运行时间或持续时间计时器。数据源和委托是承载ViewController的对象 我将标签拖到tableview中的prototype单元格中,然后使用以下代码驱动标签中的文本: 首先,tableview方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
cell.textLabel.textColor = [UIColor redColor];
NSAttributedString *attString;
attString = cell.textLabel.attributedText;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Tasks
// 1) Create new TimedActivity object timestamped now, named and categorized
// 2) Start updating label giving duration of selected activity.
// 3) Each cell must have a label
TimedActivity *previousTimedActivity = [TimedActivity MR_findFirstOrderedByAttribute:@"startTime" ascending:NO];
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
WMDGActivity *thisActivity = [actFRC objectAtIndexPath:indexPath];
//Create fresh TimedActivity object
currentTimedActivity = [TimedActivity MR_createInContext:localContext];
//Name the attributes
currentTimedActivity.name = thisActivity.name;
currentTimedActivity.category = thisActivity.category;
currentTimedActivity.startTime = [NSDate date];
previousTimedActivity.stopTime = currentTimedActivity.startTime;
//previousActivityDuration
NSTimeInterval previousActivityDuration = [previousTimedActivity.stopTime timeIntervalSinceDate:previousTimedActivity.startTime];
previousTimedActivity.duration = @(previousActivityDuration);
[localContext MR_saveToPersistentStoreAndWait];
// I put this line here to delay updates so the start time is not the same as "Now"
[self performSelector:@selector(updateTime) withObject:nil afterDelay:0.5f];
// Timer regulates the readout updates in the label
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
[self updateTime];
[self refreshData];
}
标签文本的设置如下所示:
-(void)updateTime
{
//Which calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//Gets the componentized interval from the most recent time an activity was tapped until now
NSDateComponents *components= [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:currentTimedActivity.startTime toDate:[NSDate date] options:0];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds =[components second];
if(!seconds)
{
[timer invalidate];
}
//Converts the components to a string and displays it in the duration label
self.durationLabel.text = [NSString stringWithFormat:@"%02i:%02i:%02i",hours,minutes,seconds];
NSLog(@"The label should be reading %02i:%02i:%02i",hours,minutes,seconds);
NSLog(@"Label is actually reading %@",self.durationLabel.text);
}
结果是一个空白标签,尽管时间在NSLogs中看起来是正确的:
MR_saveWithOptions:completion:]_block_invoke21(0x8db66c0) → Finished saving: <NSManagedObjectContext (0x8db66c0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2014-03-11 12:21:16.075 WMDGx[51203:a0b] The label should be reading 00:00:00
2014-03-11 12:21:16.076 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:16.575 WMDGx[51203:a0b] The label should be reading 00:00:00
2014-03-11 12:21:16.575 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:17.075 WMDGx[51203:a0b] The label should be reading 00:00:01
2014-03-11 12:21:17.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:18.075 WMDGx[51203:a0b] The label should be reading 00:00:02
2014-03-11 12:21:18.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:19.075 WMDGx[51203:a0b] The label should be reading 00:00:03
2014-03-11 12:21:19.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:20.074 WMDGx[51203:a0b] The label should be reading 00:00:04
2014-03-11 12:21:20.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:21.074 WMDGx[51203:a0b] The label should be reading 00:00:05
2014-03-11 12:21:21.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:22.074 WMDGx[51203:a0b] The label should be reading 00:00:06
2014-03-11 12:21:22.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:23.074 WMDGx[51203:a0b] The label should be reading 00:00:07
2014-03-11 12:21:23.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:24.074 WMDGx[51203:a0b] The label should be reading 00:00:08
2014-03-11 12:21:24.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:25.074 WMDGx[51203:a0b] The label should be reading 00:00:09
2014-03-11 12:21:25.075 WMDGx[51203:a0b] Label is actually reading (null)
2014-03-11 12:21:26.074 WMDGx[51203:a0b] The label should be reading 00:00:10
2014-03-11 12:21:26.075 WMDGx[51203:a0b] Label is actually reading (null)
MR_saveWithOptions:completion:][u block_invoke21(0x8db66c0)→ 已完成保存:在***主线程上***
2014-03-11 12:21:16.075 WMDGx[51203:a0b]标签上的读数应该是00:00:00
2014-03-11 12:21:16.076 WMDGx[51203:a0b]标签实际读取(空)
2014-03-11 12:21:16.575 WMDGx[51203:a0b]标签上的读数应该是00:00:00
2014-03-11 12:21:16.575 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:17.075 WMDGx[51203:a0b]标签的读数应为00:00:01
2014-03-11 12:21:17.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:18.075 WMDGx[51203:a0b]标签上的读数应该是00:00:02
2014-03-11 12:21:18.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:19.075 WMDGx[51203:a0b]标签的读数应为00:00:03
2014-03-11 12:21:19.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:20.074 WMDGx[51203:a0b]标签的读数应为00:00:04
2014-03-11 12:21:20.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:21.074 WMDGx[51203:a0b]标签的读数应为00:00:05
2014-03-11 12:21:21.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:22.074 WMDGx[51203:a0b]标签的读数应为00:00:06
2014-03-11 12:21:22.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:23.074 WMDGx[51203:a0b]标签的读数应为00:00:07
2014-03-11 12:21:23.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:24.074 WMDGx[51203:a0b]标签的读数应为00:00:08
2014-03-11 12:21:24.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:25.074 WMDGx[51203:a0b]标签的读数应为00:00:09
2014-03-11 12:21:25.075 WMDGx[51203:a0b]标签实际读数为空
2014-03-11 12:21:26.074 WMDGx[51203:a0b]标签的读数应为00:00:10
2014-03-11 12:21:26.075 WMDGx[51203:a0b]标签实际读数为空
我显然忽略了一些东西,如果历史是一个指南,那么它将是显而易见的。然而,我找不到它
有人能告诉我怎么走吗
谢谢 试试[self->tableview-reloaddata];在几个地方都试过了。还是不行,有人吗?如前所述,我尝试了@user的建议,并发现我可以影响时间更新的频率,但在任何情况下,self.durationLabel.text总是(null)。