LKM
LKM

Reputation: 2450

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;
}

Upvotes: 1

Views: 1224

Answers (3)

oks_ios
oks_ios

Reputation: 187

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;
}

Upvotes: 4

Ahsan Ebrahim Khatri
Ahsan Ebrahim Khatri

Reputation: 1847

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];

Upvotes: 1

Manuel Escrig
Manuel Escrig

Reputation: 2835

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];
}

Upvotes: 2

Related Questions