Multiple countdown timers inside a uitableview

How do I solve this problem? Basically, I will have an array of "seconds int" specified inside a UITableView. So, how can I assign each "second int" a countdown to zero? Possible problems that I see are timers that do not update when a cell has not yet been created. And how do I create several independent NSTimers updating various ui elements? I am completely lost here, so any suggestions are greatly appreciated. for visual purposes, I want to have something like this:

enter image description here

+5
source share
2 answers

From the image, it looks like your model is a set of actions that the user plans to take. I would arrange it like this:

1) MyAction - NSObject . MyAction - :

- (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 , NSArray MyActions, NSTimer ( ), , .

// 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];
}
+8

, , UITableViewCell . , TimerWentOff -. .

, . , .

0

All Articles