简体   繁体   中英

Updating text of the label in the cell in the tableview every second in iOS

I want to update text of each label in each cell in tableView every second got by calculating between two NSDate exactly like : "~days ~hours ~minutes ~seconds"

In tableview , it draws cells at cellForRowAtIndex , the delegate of tableViewController .

So I try to use timer and method reloadData: [self.tableview reloadData] in the cellForRowAtIndex , but at that time I think it makes infinite loop and dies.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(updateLabel:)
                                           userInfo:nil 
                                            repeats:YES ];

    cell.timeLeftLabel.text=[self updateLabel:self.timer]; 
}


- (NSString *)updateLabel:(id)sender {
    /*At this method , i calculate left days,hours,minutes,seconds between two nsdates
    and I make the string by those values : */

    NSLog(@"updateLabel : %@",timeLeftString);
    return timeLeftString;
}

You need to reload table, not a label or cell every second - this means - create ONE timer and call method [tableView reloadData];

- (void)viewDidLoad {

    [super viewDidLoad];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(reloadTableViewData)
                                                userInfo:nil
                                                 repeats:YES ];
}

- (void)reloadTableViewData
{
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // setup cell
    cell.timeLeftLabel.text=[self updateLabel:self.timer];
}

- (NSString *)updateLabel:(id)sender {
    /*At this method , i calculate left days,hours,minutes,seconds between two nsdates
         and I make the string by those values : */

    NSLog(@"updateLabel : %@",timeLeftString);
    return timeLeftString;
}

You can't use the [self.tableview reloadData] inside cellForRowAtIndex .

As you said it will create an infinite loop.

Instead create a timer for example in viewDidLoad like the following example:

- (void)viewDidLoad
{
 [super viewDidLoad];

 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(increaseTimerCount) userInfo:nil repeats:YES]

}

- (void)increaseTimerCount
{
  [self.tableView reloadData];
}

create an NSTimer and set it to every sec :

[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(targetMethod)
userInfo:nil
repeats:YES];

and in the targetMethod use

[myTableView reloadData];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM