更新 UITableCell 中的计时器值

发布于 2024-10-28 10:54:49 字数 1668 浏览 0 评论 0原文

我有一个计时器,它每秒调用 updateInterval 。该计时器将计算剩余时间并将结果作为 NSString。换句话说,NSString 每秒都在变化,从我下面所做的来看,UITable 然后每 1 秒重新加载一次。问题是它在 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:[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];

}

这是为什么?

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:[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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2024-11-04 10:54:49

如果您在控制台中访问不良,则可能是因为您必须在 updateInterval 方法中保留 labelText。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文