Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple countdown timers inside uitableview

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:

enter image description here

like image 826
Diffy Avatar asked Mar 29 '12 02:03

Diffy


1 Answers

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];
}
like image 167
danh Avatar answered Dec 19 '22 09:12

danh