2017-02-10 42 views
0

我的應用程序遵循方案web(json) - > appDelegate(Core Data) - > ViewController。到目前爲止,我可以看到我從序列化json獲取的位置以及我輸入用於測試的所有其他新位置。但現在我試圖在tableview中顯示它們,到目前爲止它不工作。該表爲空,雖然我知道的位置都在覈心數據:下面的代碼:IOS/Objective-C:無法顯示核心數據信息

#import "TableTableViewController.h" 
#import "DetailViewController.h" 
#import <CoreData/CoreData.h> 
#import "Spot.h" 
#import "AppDelegate.h" 




@interface TableViewController() 

@property (strong) NSMutableArray *locations; 

@end 

@implementation TableViewController 

- (NSManagedObjectContext *)managedObjectContext 
{ 
    NSManagedObjectContext *context = nil; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    if ([delegate respondsToSelector:@selector(managedObjectContext)]){ 
     context = [delegate managedObjectContext]; 
    } 
    return context; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Spot"]; 
    self.locations = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    [self.tableView reloadData]; 

    } 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 


    return self.locations.count; 

} 


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

    NSManagedObject *ls = [self.locations objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [ls valueForKey:@"name"]]]; 


    return cell; 
} 

誰能告訴我什麼我做錯了嗎?

更新: 如果我記錄位置 - > NSLog(@「%@ the locations are」,_locations);我得到一個空結果。這意味着獲取的數據不會填充數組,因此該表保持爲空,對吧?但爲什麼數組沒有被填充?

+0

我需要檢查我的數據庫是否爲空?我怎樣才能做到這一點? – FuManchu

+1

爲了進行健全性檢查,我在numberOfRowsInSection方法中設置了一個斷點,以確保它被調用,然後,如果確實碰到了,打印出self.locations.count的結果並確保它不是0 。如果該斷點跳閘並且計數大於0,則表示有數據。 – ghostatron

+0

你有沒有試過@ conarch的建議? –

回答

0

檢查您的self.tableView.dataSource是否爲零

+0

對不起,但我沒有得到它 – FuManchu

+0

- (void)viewDidLoad {super viewDidLoad]; self.tableView.dataSource = self; // add this – vaddieg

+0

不,它仍然是空的... – FuManchu