Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

countdown label in tableview cell

erdinc27erdinc27 TurkeyPosts: 396Registered Users @ @
i want to put a label in table view cell that will display a countdown from a certain time. I have another view in that cell that slides left and right. Its starting position is most right but if the user wants it then can be dragged to left. The problem i face is that lets say the user dragged that view to left and then the timer triggered a method to countdown then the view moves back to right. Here how my code looks like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"ContestsCell";
    ContestsCell *theCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    NSDictionary *currentDictionary = self.contests[indexPath.row];

    if (![[currentDictionary objectForKey:@"lastDate"] isEqualToString:@"00:00:00"]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [formatter setDateFormat:@"HH:mm:ss"];
        self.countdownDate = [formatter dateFromString:[currentDictionary objectForKey:@"lastDate"]];
        NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:formatter repeats:YES];
        [timer fire];
    return theCell;
- (void)handleTimer:(NSTimer*)theTimer {
    NSDateFormatter *formatter = (NSDateFormatter*)[theTimer userInfo];
    self.countdownDate = [self.countdownDate dateByAddingTimeInterval:-1.0];
    NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
    ContestsCell *candiateCell = [self.contestsTable cellForRowAtIndexPath:path];
    candiateCell.timeLabel.text = [formatter stringFromDate:self.countdownDate];

Why the view moves right back ? The cell is created when the timer is triggered ?


  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    I'm not sure about the specific issue, but at a glance I do not see anything in cellForRow that would restore the horizontal position of the sliding view. So perhaps the cell is being reloaded, and thus the original position is restored. You should keep track of this position when the user moves it, and then be able to restore that position if the cell is reloaded.

    I think you have some structural problems, too. You are not able to stop the timer, so it will just keep firing off forever. I'd say that most of what you have here should probably be in ContestsCell instead, with startTimer/stopTimer public methods that you can call as needed.

    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
Sign In or Register to comment.