1 #pragma mark - Table View 2 3 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 4 return 1; 5 } 6 7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 8 return self.objects.count; 9 } 10 11 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 12 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 13 14 NSDictionary *dictData = self.objects[indexPath.row]; 15 cell.textLabel.text = dictData[@"Content"]; 16 cell.detailTextLabel.text = dictData[@"CDate"]; 17 return cell; 18 } 19 20 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 21 // Return NO if you do not want the specified item to be editable. 22 return YES; 23 } 24 25 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 26 if (editingStyle == UITableViewCellEditingStyleDelete) { 27 [self.objects removeObjectAtIndex:indexPath.row]; 28 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 29 } else if (editingStyle == UITableViewCellEditingStyleInsert) { 30 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 31 } 32 } 33 34 #pragma mark -- 开始请求Web Service 35 - (void)startRequest { 36 NSString *strURL = @"http://www.51work6.com/service/mynotes/WebService.php"; 37 38 NSURL *url = [NSURL URLWithString:strURL]; 39 40 NSString *post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@", @"xxxxxxxxx@qq.com", @"JSON", @"query"]; 41 42 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; 43 44 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 45 [request setHTTPMethod:@"POST"]; 46 [request setHTTPBody:postData]; 47 48 NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 49 NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfig delegate:nil delegateQueue:[NSOperationQueue mainQueue]]; 50 51 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 52 53 NSLog(@"请求完成..."); 54 if (!error) { 55 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; 56 [self reloadView:dict]; 57 } else { 58 NSLog(@"error: %@", error.localizedDescription); 59 } 60 }]; 61 [task resume]; 62 } 63 64 #pragma mark -- 重新加载视图 65 - (void)reloadView:(NSDictionary *) res { 66 67 NSNumber *resultCode = res[@"ResultCode"]; 68 69 if ([resultCode integerValue] >= 0) { 70 self.objects = res[@"Record"]; 71 [self.tableView reloadData]; 72 NSLog(@"success"); 73 } else { 74 NSString *errorStr = [resultCode errorMessage]; 75 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"错误信息" message:errorStr preferredStyle:UIAlertControllerStyleAlert]; 76 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 77 [alertController addAction:okAction]; 78 [self presentViewController:alertController animated:TRUE completion:nil]; 79 } 80 81 } 82 @end
运行程序时,输出信息为:
1 请求完成... 2 success
直接在地址栏上敲,是可以响应的:
网站的数据是有的,读取也没有异常,请问为什么tableview上却什么数据都没有显示出来?
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
试一试
另外:http://www.cnblogs.com/CoderEYLee/p/Object-C-0018.html
确认代理遵循与否。cell注册与否。最简单的断点,确定代理走了吗