ios - 实时更新 UITableViewCell 中的 UILabel

标签 ios uitableview

请参阅下面的编辑

我正在尝试实时更新 UITableViewCell 中标签中的文本,以反射(reflect)自从通过 didSelectRowAtIndexPath 选择单元格以来耗时。

问题的症状是标签保持空白。

这是我的 updateTime 方法中的代码:

-(void)updateTime
{


    //Which calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //Gets the componentized interval from the most recent time an activity was tapped until now
    NSDateComponents *components= [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:currentTimedActivity.startTime toDate:[NSDate date] options:0];

    NSInteger hours = [components hour];
    NSInteger minutes = [components minute];
    NSInteger seconds =[components second];
//    
    if(!seconds)
    {
        [timer invalidate];

    }
    //Converts the components to a string and displays it in the duration label
    cellLabelTempText = [NSString stringWithFormat:@"%02i:%02i:%02i",hours,minutes,seconds];
    self.durationLabel.text = cellLabelTempText;

    NSLog(@"The label should be reading %02i:%02i:%02i",hours,minutes,seconds);
    NSLog(@"cellLabelTempText is reading %@",cellLabelTempText);
    NSLog(@"The durationLabel should read the same, as they are equal.");
    NSLog(@"The durationLabel is actually showing %@",self.durationLabel.text);


    [self.myTableView reloadData];

}

我的 NSLog 指令产生以下结果:

2014-03-12 14:48:57.433 WMDGx[69802:a0b] The label should be reading 00:00:00
2014-03-12 14:48:57.434 WMDGx[69802:a0b] cellLabelTempText is reading 00:00:00
2014-03-12 14:48:57.435 WMDGx[69802:a0b] The durationLabel should read the same, as they are equal.
2014-03-12 14:48:57.435 WMDGx[69802:a0b] The durationLabel is actually showing (null)
2014-03-12 14:48:57.933 WMDGx[69802:a0b] The label should be reading 00:00:01
2014-03-12 14:48:57.934 WMDGx[69802:a0b] cellLabelTempText is reading 00:00:01
2014-03-12 14:48:57.934 WMDGx[69802:a0b] The durationLabel should read the same, as they are equal.
2014-03-12 14:48:57.935 WMDGx[69802:a0b] The durationLabel is actually showing (null)
2014-03-12 14:48:58.933 WMDGx[69802:a0b] The label should be reading 00:00:02
2014-03-12 14:48:58.934 WMDGx[69802:a0b] cellLabelTempText is reading 00:00:02
2014-03-12 14:48:58.934 WMDGx[69802:a0b] The durationLabel should read the same, as they are equal.
2014-03-12 14:48:58.935 WMDGx[69802:a0b] The durationLabel is actually showing (null)
2014-03-12 14:48:59.933 WMDGx[69802:a0b] The label should be reading 00:00:03
2014-03-12 14:48:59.934 WMDGx[69802:a0b] cellLabelTempText is reading 00:00:03
2014-03-12 14:48:59.934 WMDGx[69802:a0b] The durationLabel should read the same, as they are equal.
2014-03-12 14:48:59.934 WMDGx[69802:a0b] The durationLabel is actually showing (null)
2014-03-12 14:49:00.933 WMDGx[69802:a0b] The label should be reading 00:00:04
2014-03-12 14:49:00.933 WMDGx[69802:a0b] cellLabelTempText is reading 00:00:04
2014-03-12 14:49:00.934 WMDGx[69802:a0b] The durationLabel should read the same, as they are equal.
2014-03-12 14:49:00.934 WMDGx[69802:a0b] The durationLabel is actually showing (null)

我很困惑,因为临时字符串 cellLabelTempText 正在正确更新,并且 self.durationLabel.text 等于 cellLabelTempText,但注册作为日志中的 (null)

非常感谢所有帮助!

根据@rmaddy 在下方评论中的建议进行编辑(请阅读):

因为我的 self.durationLabel 显示的是 nil,所以我尝试连接到 IBOutlet,但出现此错误:

... /WMDGx/MainStoryboard.storyboard: Couldn't compile connection: <IBCocoaTouchOutletConnection:0x7ff6a28f8560  <IBProxyObject: 0x7ff6a28f55e0> => durationLabel => <IBUILabel: 0x7ff6a28f34a0>>

这很好地表明了问题出在哪里,但是如果整个事情都依赖于 IBOutlet,而我无法将它连接到一个,那么我该从哪里去呢?

谢谢!

最佳答案

您的问题是您没有单一的 durationLabel;您的表格中有多少个单元格。 (错误具体是试图将 tableCell 原型(prototype)连接到特定标签)。我建议对 UITableviewCell 进行子类化,添加一个 durationLabel 标签和属性(可以链接到原型(prototype)单元格),并将这个计时器代码放在该子类中。

像这样:

在你的 tableViewController 中:

//At the top:
#import "PTCell.h"

//replace didSelectRowAtIndexPath:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PTCell * cell = (PTCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell startTimer];
}

PTCell.h:

#import <UIKit/UIKit.h>

@interface PTCell : UITableViewCell;
@property (strong, nonatomic) IBOutlet UILabel *durationLabel;
@property (nonatomic, strong) NSDate * startTime;
@property (nonatomic, strong) NSTimer * timer;

-(void) startTimer;
-(void) stopTimer;

@end

PTCell.m:

#import "PTCell.h"

@implementation PTCell

-(void)updateTime {

    //Which calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //Gets the componentized interval from the most recent time an activity was tapped until now
    NSDateComponents *components= [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:self.startTime toDate:[NSDate date] options:0];

    NSInteger hours = [components hour];
    NSInteger minutes = [components minute];
    NSInteger seconds =[components second];
    //
    if(!seconds)
    {
        [self.timer invalidate];

    }
    //Converts the components to a string and displays it in the duration label
    NSString * cellLabelTempText = [NSString stringWithFormat:@"%02i:%02i:%02i",hours,minutes,seconds];
    self.durationLabel.text = cellLabelTempText;

    NSLog(@"The label should be reading %02i:%02i:%02i",hours,minutes,seconds);
    NSLog(@"cellLabelTempText is reading %@",cellLabelTempText);
    NSLog(@"The durationLabel should read the same, as they are equal.");
    NSLog(@"The durationLabel is actually showing %@",self.durationLabel.text);

}

-(void) startTimer {
    [self stopTimer];
    self.startTime = [NSDate date];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}

-(void) stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

@end

关于ios - 实时更新 UITableViewCell 中的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22364824/

相关文章:

ios - 当我关闭应用程序时,我收到来自调试器 : Terminated due to signal 15 的消息

ios - 如何从Bundle获取PNG文件的资源路径?

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

ios - Swift(解析)顶部 UITableViewCell 的新行?

ios - AutoLayout 问题 UITable View 标题部分 : Adding image in the left does not works fine

iphone - 在我的 View 中获取分组的表格 View 背景颜色

ios - 如何将单元格添加到 UITableView 部分并保存核心数据信息

iphone - Mapkit addAnnotation异常: unrecognized selector sent to instance

iphone - 根据按下的按钮将数据传递到 tableViewController

swift - 试图找到一种更好的方法将 API JSON 映射到不同的结构取决于 swift 中选择的类别