Reputation: 166
I am getting millisecond and i want to convert that millisecond to day,hours,minutes,second and display and want to display it in uiTableview. and i want to update that timer every second..
i had try this code using uiTableview delegate method cellForRowAtIndexPath
.
NSInteger totleTime = [[[productListDataArray objectAtIndex:indexPath.row]objectForKey:@"_auction_duration"] integerValue];
totleTime = totleTime - timerValue ;
long days = (totleTime) / (60 * 60 * 24);
long hours = (totleTime - days * 60 * 60 * 24) / (60 * 60);
long minuts = (totleTime - days * 60 * 60 * 24 - hours * 60 * 60) / 60;
long seconds = (totleTime - days * 60 * 60 * 24 - hours * 60 *60 - minuts * 60);
NSLog(@"seconds : %.f minutes : %.f hours : %.f days : %.f ", seconds, minuts, hours, days);
I had used this method for call method and update tableview. but its not working so suggest me another option or tell me what i had done wrong in this code..
Upvotes: 2
Views: 562
Reputation: 66
I don't know timeValue's meanings, if timeValue assume system time. you can write a timer to reload tableView's data.
Upvotes: -1
Reputation: 3802
What I understand from your question is that you want to reload your tableView every second. First of all, let me say that it is a bad idea. If your tableView is being reloaded then the user will not be able to see anything. Having said that, you can use a timer with a certain interval that calls a method. That method can decide when to reload the tableView.
EDIT
From your last comment, I understand that you are trying to display a countdown timer in a tableview cell. For that purpose, you don't need to reload the tableview. You only have to update the datasource and reload the row that is displaying the counter. Something along these lines.
The code snippet below is in Swift 2
//Call a method named tick() every second
let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
func tick() {
//DO YOUR CALCULATIONS HERE TO CALCULATE YOUR DAYS, MINUTES, etc. THEN CALL THE FOLLOWING METHOD
tblView.reloadRowsAtIndexPaths([NSIndexPath(forRow: {rowNumber}, inSection: {sectionNumber})], withRowAnimation: .None)
//ALSO MAKE SURE YOUR cellForRowAtIndexPath IS CORRECTLY SETTING THE CELL VALUES
}
Don't forget to invalidate
the timer when you're finished with it
Upvotes: 2
Reputation: 166
i have get solve this issues.
1.At initial i had set timer is 1 second.
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
2.Every Second Call Method and change value and reload tableview.
- (void)updateCounter:(NSTimer *)theTimer {
timerValue += 1;
[ProductListTableView reloadData];
}
3. now calculate remaining time in uitableView deleget method cellForRowAtIndexPath
NSInteger totleTime = [[[productListDataArray objectAtIndex:indexPath.row]objectForKey:@"_auction_duration"] integerValue];
totleTime = totleTime - timerValue ;
long days = (totleTime) / (60 * 60 * 24);
long hours = (totleTime - (days * 60 * 60 * 24)) / (60 * 60) ;
long minuts = (totleTime - (days * 60 * 60 * 24) - (hours * 60 * 60)) / 60;
long seconds = (totleTime - (days * 60 * 60 * 24) - (hours * 60 * 60) - (minuts * 60));
cell.saleTimeLabel.text = [NSString stringWithFormat:@"%@ Sold %ld:%ld:%ld:%ld",[[productListDataArray objectAtIndex:indexPath.row]objectForKey:@"sales_qty"], days,hours,minuts,seconds];
Upvotes: 1