0

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

附上代码

#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
4

1 回答 1

0

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

于 2018-02-23T15:10:42.790 回答