How do I go about this task? Basically, I will have an array of "seconds int" listed inside the UITableView. So how can I assign each "seconds int" to countdown to zero? The possible problems I'm seeing is the timers not being updated when the cell is not yet created. And how do I instantiate multiple independent NSTimers updating different ui elements? I'm quite lost here, so any suggestions is greatly appreciated. for visual purposes, I want to have something like this:
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
From the image, it looks like your model is a set of actions the user plans to take. I would arrange things this way:
1) MyAction is an NSObject with a name and a due date. MyAction implements something like this:
- (NSString *)timeRemainingString {
NSDate *now = [NSDate date];
NSTimeInterval secondsLeft = [self.dueDate timeIntervalSinceDate:now];
// divide by 60, 3600, etc to make a pretty string with colons
// just to get things going, for now, do something simple
NSString *answer = [NSString stringWithFormat:@"seconds left = %f", secondsLeft];
return answer;
}
2) StatusViewController keeps a handle to the model which is an NSArray of MyActions, it also has an NSTimer (just one) that tells it time is passing.
// schedule timer on viewDidAppear
// invalidate on viewWillDisappear
- (void)timerFired:(NSTimer *)timer {
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.model.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyAction *myAction = [self.model objectAtIndex:indexPath.row];
// this can be a custom cell. to get it working at first,
// maybe start with the default properties of a UITableViewCell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myAction timeRemainingString];
cell.detailTextLabel.text = [myAction name];
}
回答2:
To have fancy UI stuff in the table like that, you might want to subclass UITableViewCell and create a timer as a property in each. The cell can then own a delegate which responds to TimerWentOff and handles ringing of an alarm or a notification or whatnot. Each timer would update the label in it's cell.
Hope that helps a bit. If you have more specific questions, let me know.