2016-08-05 9 views
0

私はUIViewControllerでテーブルビューを持っており、データベースからデータをロードします。しかし、私はアプリを起動すると、テーブルは空です。テーブルをクリックしたときにのみ、データが表示されます。誰かが私が何を変えなければならないと教えてくれる?ここに私のコードは次のとおりです。Objective-C:UIViewControllerのTableView - データはクリック後にロードされます。

#import "DashboardViewController.h" 
#import <UIKit/UIKit.h> 

@interface DashboardViewController() 

@property (strong, nonatomic) IBOutlet UITableView *tableView; 
@property (strong, nonatomic) NSMutableArray *data; 

@end 

@implementation DashboardViewController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

} 

-(void)viewWillAppear:(BOOL)animated{ 
    [self data]; // ??? 
} 

- (NSMutableArray *)data { 

    if(!_data){ 

     _data = [[NSMutableArray alloc] init]; 

     NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"]; 

     NSURLSession *session = [NSURLSession sharedSession]; 
     NSString *url = [NSString stringWithFormat:@"http://www.example.net/getData.php?user=%@", username]; 
     NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ 

      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      NSString *errorCode = [json valueForKey:@"error"]; 

      if([errorCode isEqualToString:@"e0"]){ 

       NSArray *myData = [json objectForKey:@"myData"]; 

       for (int i = 0; i < [myData count]; i++) { 
        [_data addObject:[myData objectAtIndex:i]]; 
        [self.tableView reloadData]; 
       } 

      } 

     }]; 

     [dataTask resume]; 

    } 

    return _data; 

} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.data count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 

    cell.textLabel.text = [[self.data objectAtIndex:indexPath.row] objectForKey:@"name"]; 

    return cell; 
} 

@end 
+0

のようにちょうど[self.tableView reloadData]書くありません。あなたのループから外れ、条件が満たされている場合。 –

答えて

1

for (int i = 0; i < [myData count]; i++) { 
       [_data addObject:[myData objectAtIndex:i]]; 
       [self.tableView reloadData]; 
      } 

が好きではありませ

for (int i = 0; i < [myData count]; i++) { 
       [_data addObject:[myData objectAtIndex:i]]; 

      } 

      if (_data.count>0) 
     { 
     dispatch_async(dispatch_get_main_queue(), ^(void){ 
     //Run UI Updates 
      [self.tableView reloadData]; 
     }); 
     } 
+0

メインスレッドのUIを取る必要があります。なぜなら、メイン 'dataTaskWithURLに別のスレッドを追加した理由なので、' dispatch_get_main_queue'を使用すると、自動的にtehリスト –

+0

Tyが読み込まれます。もう一つの質問:プッシュセグを使ってそのビューにボタンがあります。そのボタンをクリックして戻るボタンを使用すると、テーブルビューのデータをどのようにリロードできますか?つまり、データベースからデータを再度ロードする必要があります。知っていますか? – Nono

+0

ViewWillAppearでデータ読み込みメソッドを呼び出す –

関連する問題