awakeFromNib返回空UILabel值

时间:2018-02-23 15:04:38

标签: objective-c uitableview uilabel awakefromnib

我在表格视图中使用自定义单元格。我有一个视图,我在该视图中有一个按钮,当单元格加载时,它检查数组是否包含单元格内的标签文本。当我尝试获取标签的文本时,它返回一个空值,如果我单击具有相同方法的按钮来获取文本,它就可以正常工作并给我标签的文本。

附件是代码

#import "TableViewCellSchedule.h"

@implementation TableViewCellSchedule
@synthesize name = _name;
@synthesize time = _time;
@synthesize pic = _pic;
@synthesize text = _text;
@synthesize scheduleID = _scheduleID;
NSMutableArray *favid;

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    favid = [[NSMutableArray alloc] init];
    NSMutableArray *temparray = [userDefaults objectForKey:@"favid"];
    if(temparray.count > 0){
        favid = [temparray mutableCopy];
        if([temparray containsObject:self.scheduleID.text]){
            NSLog(@"set image here, id is: %@", self.scheduleID.text);
        }
        else{
            NSLog(@"change image back here, id is: %@", self.scheduleID.text);
        }
    }
    NSLog(@"welcome, array is: %@", favid);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)addfav {

    if([favid containsObject:self.scheduleID.text]){
        NSLog(@"contained already");
    }
    else{
         NSLog(@"not contained");
        [favid addObject:self.scheduleID.text];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:favid forKey:@"favid"];
        [userDefaults synchronize];
    }
    //NSLog(@"schedule id is: %@", self.scheduleID.text);
    //NSLog(@"fav log: %@", favid);
}
@end

1 个答案:

答案 0 :(得分:0)

这是因为尚未构造scheduleID标签并为其赋值,您可以通过创建一个属性来保存相同的值并将其设置为cellForRowAt OR 在cellForRow中移动整个检查代码,并将应该直接分配给标签的值与数组进行比较