问题描述
我该如何完成这项任务?基本上,我将在UITableView中列出一个seconds int数组。那么如何将每个秒int分配给倒计时到零?我看到的可能的问题是,当尚未创建单元格时,计时器未被更新。如何实例化多个独立的NSTimers更新不同的ui元素?我在这里很丢失,所以非常感谢任何建议。出于视觉目的,我想要这样的东西:
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:
推荐答案
从图像看,您的模型看起来像是用户计划的一组操作采取。我会这样安排:
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是一个名字和截止日期的NSObject。 MyAction实现如下:
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保留模型的句柄,该模型是MyActions的NSArray,它还有一个NSTimer(只有一个)告诉它时间正在过去。
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];
}
这篇关于uitableview内的多个倒计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!