我想设置在用户在我的tableview中点击的任何单元格内的标签内运行的经过时间或持续时间计时器。 dataSource和delegate是托管ViewController。
我将一个标签拖到tableview中的原型单元格中,然后使用以下代码来驱动标签中的文本:
首先,tableview方法:
- (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)
我显然忽视了某些事情,如果历史是一个指南,那将是显而易见的事情。但是,我找不到它。
有人可以给我指路吗?
谢谢!