开发者

updating timer value in a UITableCell

开发者 https://www.devze.com 2023-02-20 21:27 出处:网络
I have a timer in which it calls the updateInterval every second. This timer will calculate the time remaining and put the result as a NSString. In other words the NSString is changing every second an

I have a timer in which it calls the updateInterval every second. This timer will calculate the time remaining and put the result as a NSString. In other words the NSString is changing every second and from what I did below, the UITable is then reloaded every 1 second. The issue is that it crashes at countdown.text = labelText;

  // inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//blah
 else if (indexPath.row == 1 && indexPath.section == 0){
        UILabel * countdown = [[UILabel alloc] init];
        countdown.text = labelText;
        [cell.contentView addSubview:countdown];
        cell.textLabel.text = @"Time remaining";
    }
//blah

- (void)updateInterval:(NSTimer *)timer {
    NSTimeInterval timeinterval = [(NSDate *)[data valueForKey:@"start_date"] timeIntervalSinceDate开发者_如何学C:[NSDate date]];

    // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];

    // Create the NSDates
    NSDate *date1 = [[NSDate alloc] init];
    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:timeinterval sinceDate:date1]; 

    // Get conversion to months, days, hours, minutes
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

    labelText = [NSString stringWithFormat:@"%d day %d:%d:%d", [breakdownInfo day], [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]];
    //NSLog(@"%d day %d:%d:%d", [breakdownInfo day], [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]);
    [self.table reloadData];
    [date1 release];
    [date2 release];

}

Why is this?


If you are getting bad access in console then it may be because you have to retain labelText in your updateInterval method.

0

精彩评论

暂无评论...
验证码 换一张
取 消