multiple countdown timers inside uitableview

邮差的信 提交于 2019-12-07 06:19:58

问题


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.



来源:https://stackoverflow.com/questions/9918472/multiple-countdown-timers-inside-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!