我正在研究这个应用程序,但不知何故,它没有返回我的tableviewcontroller中的任何行。 这是我的代码:
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kStudentenURL [NSURL URLWithString:@"http://localhost/api/api.php"]
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController () {
NSArray *_studenten; } @end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib]; }
- (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy) HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD]; // Regiser for HUD callbacks so we can remove it from the window at the right time HUD.delegate = self; // Show the HUD while the provided method executes in a new thread [HUD showWhileExecuting:@selector(getJsonDataFromServer) onTarget:self withObject:nil animated:YES]; }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
-(void)getJsonDataFromServer {
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kStudentenURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
}); }
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
_studenten = [json objectForKey:@"studenten"];
NSLog(@"Studenten: %@", _studenten);
NSLog(@"%u", _studenten.count); }
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _studenten.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *student = [_studenten objectAtIndex:0];
NSString *studentNaam = [student objectForKey:@"studentNaam"];
NSString *studentAchterNaam = [student objectForKey:@"studentAchterNaam"];
cell.textLabel.text = studentAchterNaam;
cell.detailTextLabel.text = studentNaam;
return cell; }
/* // Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { }
*/
/* // Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES; }
*/
/*- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
} }*/
@end
我知道我的json正确地进入了。 NSLog
s正在返回我要求的数据,但我似乎无法获得任何行。有人可以帮我一把吗? TNX
答案 0 :(得分:0)
简单回答 - 一旦你加载了那个数据数组,你需要调用[(你的tableview)reloadData]!
据推测,你现在已经在故事板上获得了你的桌面视图,并且你已经设置了它的数据源并委托给你的视图控制器。您还需要在视图控制器中为该tableview提供一个属性。您可能有一些看起来像这样的代码。
@interface MasterViewController () {
NSArray *_studenten;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation MasterViewController
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
_studenten = [json objectForKey:@"studenten"];
NSLog(@"Studenten: %@", _studenten);
NSLog(@"%u", _studenten.count);
[self.tableView reloadData];
}
@end
答案 1 :(得分:0)
我的猜测是你永远不会从dequeueReusableCell
返回一个有效的单元格。我建议你做的是在尝试重用一个单元格之后,检查它是否为零,如果需要,你需要分配一个新的单元格。我已将代码添加到您的函数中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
NSDictionary *student = [_studenten objectAtIndex:0];
NSString *studentNaam = [student objectForKey:@"studentNaam"];
NSString *studentAchterNaam = [student objectForKey:@"studentAchterNaam"];
cell.textLabel.text = studentAchterNaam;
cell.detailTextLabel.text = studentNaam;
return cell; }