【问题标题】:NSMutableArray property automatically reset in TableViewControllerNSMutableArray 属性在 TableViewController 中自动重置
【发布时间】:2013-02-07 00:49:09
【问题描述】:

我目前正在尝试从 NSNotification 更新 TableViewController 上的 NSMutableArray 属性,但遇到问题。

我在 Observer 类 .h 文件中声明了我的属性,如下所示:

@property (nonatomic,strong) NSMutableArray *cart;

在 Observer 类 .m 文件中合成:

@synthesize cart = _cart;

我在 Observer 类的 AwakeFromNib 方法中收到通知:

- (void)awakeFromNib{

if (!self.cart){
    NSLog(@"self.cart == nil");
    self.cart = [[NSMutableArray alloc] init];
}else{
    NSLog(@"self.cart != nil");
}

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCurrentItemToCartFromNotification:) name:@"ItemAddedToCart" object:nil];
}

请注意,在收到通知之前,我正在上述 AwakeFromNib 方法中执行我的 NSMutableArray 属性的 alloc init。

这是收到通知时调用的方法:

- (void)addCurrentItemToCartFromNotification:(NSNotification *)notification{

 NSDictionary *currentItem = [notification.userInfo objectForKey:@"CART_ITEM_INFORMATION"];

[self.cart addObject:currentItem];
[self.tableView reloadData];

}

然后,我拥有基于我的 NSMutableArray 属性的 tableview 数据源方法,该属性在通知中的上述方法中更新。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.cart count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"itemInCart";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSDictionary *cartItem = [self.cart objectAtIndex:indexPath.row];
cell.textLabel.text = [cartItem objectForKey:@"ITEM_NAME"];

return cell;
}

我对程序的预期行为是在收到通知时更新我的​​ NSMutable 数组属性(由于 if (!self.cart) 条件,alloc init 应该只在第一次发生)

但是每次我收到通知时,都会删除 NSMutableArray 中的对象并添加新对象而不是追加对象。因此,在任何时间点,NSMutableArray 都只包含最近一次通知接收到的对象。

我认为 alloc init 每次都发生,而不是第一次发生。

你能告诉我我在这里缺少什么吗?非常感谢您对此问题的意见。

谢谢, 迈克

【问题讨论】:

  • 您需要先设置数组,然后再将其放入文件中。
  • 我看不出这段代码有什么问题。尝试在向 self.cart 添加项目后立即记录它,然后查看它显示的内容。
  • 另外,您是否看到“self.cart == nil”被多次打印(或“self.cart != nil”,就此而言。awakeFromNib 应该只被调用一次,除非您的类正在被释放)?
  • @rdelmar - 每次向其中添加项目时,我都会看到“self.cart == nil”。
  • 这意味着这个类正在被释放。你是如何创建这个类的?你是从另一个类分配初始化它吗?

标签: ios objective-c nsmutablearray uitableview nsnotificationcenter


【解决方案1】:

不确定为什么您会看到该数组被重新分配(如果这是发生的事情),但这无论如何都需要不同的模式:我会通过替换合成的 setter 来延迟初始化您的购物车属性...

- (NSArray *)cart {
    if (!_cart) {
        _cart = [NSMutableArray array];
    }
    return _cart;
}

删除 awakeFromNib 中的购物车内容,并始终引用 self.cart(init 和 dealloc 除外)。

【讨论】:

  • 我仍然面临同样的问题。不过有几个问题:1)整个事情都在选项卡视图cntrlr中是否重要,我正在从选项卡A发送NSNotfctn,并且上面所有用于接收NSNotfctn的代码,添加到数组等都发生在tableviewcontroller中是标签 B? 2)我在您的购物车方法中添加了一个 NSLog,如下所示:- (NSArray *)cart { if (!_cart) { NSLog(@"self.cart == nil"); _cart = [NSMutableArray array]; } NSLog(@"self.cart != nil"); return _cart; } 现在每次我发布通知时,NsLog 都会显示“self.cart!= nil”。
  • 应该如此,对吧?您可以通过记录购物车本身来帮助您进行调试,例如 NSLog(@"cart is %@", _cart);添加项目后立即记录它,然后将其记录在您的 numberOfRowInSection 方法中。计数是否一致?数组的地址有变化吗?
【解决方案2】:

每次添加条目时都会记录“self.cart == nil”这一事实意味着每次添加条目时都会调用 awakeFromNib,这反过来意味着您正在创建一个新实例每次的观察者类。所以这就是问题所在,而不是您帖子中的任何代码。为了解决这个问题,我们需要知道你是如何创建这个类的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多