0

我正在开发这个应用程序,但不知何故,它没有在我的 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 输入正确。s 正在返回我要求的NSLog数据,但我似乎无法获得任何行。有人可以帮帮我吗?tnx

4

2 回答 2

0

简单的答案——一旦你完成了数据数组的加载,你需要调用 [(your 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
于 2012-10-01T20:13:00.740 回答
0

我的猜测是,您永远不会从dequeueReusableCell. 我建议你在尝试重用一个单元格后,检查它是否为 nil,如果是,则需要分配一个新单元格。我已将代码添加到您的函数中。

- (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; }
于 2012-10-01T20:34:30.300 回答