【问题标题】:Obj-c - reload tableView with updated NSMutableArrayObj-c - 使用更新的 NSMutableArray 重新加载 tableView
【发布时间】:2019-07-16 20:31:37
【问题描述】:

我正在尝试使用新数据(self.filtered,一个 NSMutableArray)重新加载我的 tableView。为了做到这一点,我的理解是我需要从原始 self.filtered 中删除所有对象,然后用更新的 NSMutableArray 重新填充它。但是,由于某种原因,当我运行以下命令时,出现以下崩溃错误(是的,所有值都按应有的方式填充......例如,没有空响应):

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[__NSArrayI removeAllObjects]: 无法识别的选择器发送到实例 0x28009c540'

Viewcontroller.h

@property (nonatomic, strong) NSMutableArray *filtered;

ViewController.m

-(void)viewDidLoad {
    NSString *myID = [session user][@"user"][@"uid"];

    NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid ==[cd] %@", targetedUser];
    NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", myID];
    NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid ==[cd] %@", myID];
    NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", targetedUser];
    NSPredicate *p5 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", universal];

    NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];
    NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4]];
    NSCompoundPredicate * pIntermediary3 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p5]];
    NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2, pIntermediary3]];

    self.filtered = [self.messages filteredArrayUsingPredicate:pFinal];
}

- (IBAction)sendReply:(id)sender {
    NSMutableArray *aArr = [NSMutableArray arrayWithObjects:self.filtered,nil];
    NSLog(@"BEFORE IT %@", aArr);

    [aArr addObject:nodeData];

    [self.filtered removeAllObjects];
    [self.filtered addObject:aArr];

    [self.tableView reloadData];
    NSLog(@"LASTLY %@", self.filtered);
}

此方法中没有 NSArrays - 只有 NSMutableArrays,所以我不确定为什么会这样?

【问题讨论】:

  • 您的 filtered 属性包含不可变数组,而不是可变数组,因此会出现错误。展示您如何声明该属性并展示您如何为其赋值。
  • BTW - [self.filtered addObject:aArr]; 行将整个数组添加到数组中。它不会将aArr 的对象直接添加到filtered。换句话说,你留下了一个数组数组。这就是你想要的吗?
  • 仔细查看您的错误消息,发现您正在尝试调用removeAllObjects 来对抗NSArray,而不是您所期望的NSMutableArray
  • @rmaddy 查看更新
  • 啊@CodeBender 见上面的编辑。那么在 viewDidLoad 结束时,我的 self.filtered 变得不可变了吗?我怎样才能让它再次可变?

标签: ios objective-c


【解决方案1】:

行:

self.filtered = [self.messages filteredArrayUsingPredicate:pFinal];

给你一个NSArray,而不是NSMutableArray

将其更改为:

self.filtered = [[self.messages filteredArrayUsingPredicate:pFinal] mutableCopy];

然后您的代码会运行得更好。在任何试图设置 filtered 属性的行上检查这一点。

与该问题不同的是一行:

[self.filtered addObject:aArr];

这是将数组添加到数组中,而不是直接将aArr的对象添加到filtered

要么将此行更改为:

[self.filtered addObjectsFromArray:aArr];

或替换行:

[self.filtered removeAllObjects];
[self.filtered addObject:aArr];

与:

self.filtered = aArr;

【讨论】:

  • 更新了!也就是说,当我在最后重新加载我的 tableview 时,数据仍然没有更新:/ 这是为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 2021-09-29
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多