我正在尝试通过将self.neighbourData过滤到self.closeByNeighbours(一个新数组)中来创建一个新数组,并且一切正常,数据将在viewDidLoad中显示。但是,当尝试返回self.closeByNeighbours的单元格数量时,NSArray计数是否返回NULL?为什么会这样?
ViewController.h
@property (strong, nonatomic) NSArray *closeByNeighbours;
@property (strong, nonatomic) NSMutableArray *neighbourData;
ViewController.m
-(void)viewDidLoad {
NSMutableDictionary *viewParams = [NSMutableDictionary new];
[viewParams setValue:@"u000" forKey:@"view_name"];
[DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.neighbourData = [responseObject mutableCopy];
[self.neighboursView reloadData];
NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];
DIOSSession *session = [DIOSSession sharedSession];
[session setUser:userDictInfo];
[session user];
NSString *myData = [session user][@"user"][@"field_province"][@"und"][0][@"safe_value"];
self.closeByNeighbours = [self.neighbourData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(province contains[c] %@)", myData]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", [error localizedDescription]);
}];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.neighboursView) {
return [self.closeByNeighbours count];
}
}
答案 0 :(得分:2)
该框架确实在视图加载后隐式重新加载了表视图。
那时closeByNeighbours
被声明但未初始化,因此它是nil
。
viewGet
中的完成块将在稍后调用。
避免使用nil
的解决方案是在viedDidLoad
的开头初始化数组
-(void)viewDidLoad {
[super viewDidLoad];
self.closeByNeighbours = [NSArray array];
...
并且您必须在完成处理程序中的主线程上重新加载表视图
...
self.closeByNeighbours = [self.neighbourData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(province contains[c] %@)", myData]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
答案 1 :(得分:1)
我认为您忘记了初始化closeByNeighbours
。尝试添加。
NSString *myData = [session user][@"user"][@"field_province"][@"und"][0][@"safe_value"];
self.closeByNeighbours = [NSArray array: filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(province contains[c] %@)", myData]];
或者,根据需要,您只需要初始化closeByNeighbours
属性。