@property (nonatomic, copy) NSMutableArray *dataArray;
@end
@implementation EventViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CNCTableView *cncTableView = [[CNCTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
cncTableView.delegate = self;
cncTableView.dataSource = self;
[self getEventFromDB];
[self.view addSubview:cncTableView];
}
– (void)getEventFromDB{
NSURL *url = [NSURL URLWithString:@”http://192.168.1.106/cncevent/getdata.php”];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
[dataTask resume];
}
– (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
NSLog(@”Did Receive Data”);
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
for (NSDictionary *dict in array){
[self.dataArray addObject:dict];
}
}
代码如上 可是urlsessiondatataskdidreceivedata这个代理方法没有响应 这句did receive data一直没有输出
10
NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (!error) { // TODO } }];
使用这个方法,看看你的completionHandler能否调用?
本人猜测是只实现了数据下载成功的delegate方法,而没有考虑失败的情况
10