1

我正在努力理解这一点,希望有人可以帮助我解决这个问题。

我的 ViewController 中有一个名为回复的属性。这在用户单击表中的一行后由另一个 ViewController 加载,并将 XML 节点传递给新 ViewController 上的属性。

我有这个代码:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (replies == nil)
    {
        replies = [[NSArray alloc] init];
    }
    if(replies.count == 0)
    {
        replies = [node elementsForName:@"question"];
    }

    return replies.count;
}

Replies 是 ViewController 上的一个属性。NumberOfRowsInSection 在加载过程中被调用两次。在第一次通过时,我可以看到该属性已被填充。在第二个它是空的,并且对replies.count 的调用返回一个“消息发送到解除分配的实例”错误。

我对此感到有些困惑——因为该属性是在第一次通过时设置的,所以在第二次通过时我的值发生了什么变化?这一直让我发疯...

4

1 回答 1

0

确保您的财产保留对回复的强烈引用:

@property (nonatomic, strong) NSArray *replies;

并参考如下回复:

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (self.replies == nil)
  {
      self.replies = [[NSArray alloc] init];
  }
  if(replies.count == 0)
  {
      self.replies = [node elementsForName:@"question"];
  }
  return self.replies.count;
}
于 2013-05-17T15:57:15.490 回答