【问题标题】:NSArray returns NULL?NSArray 返回 NULL?
【发布时间】:2015-05-01 15:00:08
【问题描述】:

我遇到了一个奇怪的问题。我正在尝试将数组(storageData)设置为从端点(responseObject)发回的相等数据。我已经记录了 responseObject,并且数据在那里,但是由于某种原因,当我尝试为 storageData 设置它时,storageData 返回 NULL(即使我已经声明 storageData 在我的块之外使用)。有谁知道为什么会这样?见代码:

.h

@property (strong, nonatomic) NSArray *storageData;

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

[DIOSNode nodeIndexWithPage:@"0" fields:@"title" parameters:[NSArray arrayWithObjects:@"storage_item", nil] pageSize:@"20" success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Nodes retrieved!");

    self.storageData = responseObject;

    [self.tableView reloadData];
    NSLog(@"%@",storageData);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure
}];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";

    StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

       NSDictionary *temp = [storageData objectAtIndex:indexPath.row];
       NSString *title = [temp objectForKey:@"title"];
      [[cell itemName] setText:title];

    //  NSLog(@"%@", storageData);

    }
    return cell;
}

【问题讨论】:

  • 为什么 storageData = [NSArray new];有吗?
  • 您正在异步回调中设置 storageArray。它不会在函数返回时设置,而是在将来的某个时候设置。所以要准备好它是 nil,并在数据到达时重新加载表视图。

标签: ios objective-c


【解决方案1】:

nodeIndexWithPage:... 可能异步执行。

它在获取数据之前返回,然后您的表视图方法被硬连接到 5 行,因此,表会尝试填充尚未加载的数据。

让您的numberOfRowsInSection: 方法返回[storageData count]

您已经在完成块中调用了 -reloadData,因此表格将在成功加载时自动刷新。


你也是手动声明实例变量吗?

如果是这样,那么 storageData = ...self.storageData = 实际上不会设置相同的东西(除非您还覆盖 -storageData 和 -setStorageData:)。

让所有内容都通过self.storageData引用它。


您确定只使用显示数据的类的一个实例吗?

即将NSLog(@"%p", self); 添加到viewDidLoad 的开头。希望它只记录一次。它应该只打印一个十六进制数字。

NSLog() 总是打印一些东西。如果它没有打印任何东西,那么代码就没有被执行。

【讨论】:

  • 同意。 numberOfRowsInSection 的实现不应该写成返回一个硬编码的数字。
  • @rmaddy 我更改了 numberofRowsInSection 以返回 storageData 计数。我不再收到错误消息,但 storageData 仍然为空?
  • 在完成处理程序中重新加载表后为零?
  • 尝试将所有对storageData 的引用更改为self.storageData
  • @rmaddy 试过了;仍然为空。
【解决方案2】:

您的 tableView:numberOfRowsInSection: 方法应该返回 storageData.count 而不是硬编码的 5。

【讨论】:

  • 更改 numberofRowsInSection 以返回 storageData.count。我不再收到错误消息,但 storageData 仍然为空?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
相关资源
最近更新 更多