【发布时间】: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