2013-10-05 51 views
-2

我正在做一個卡拉OK項目。我想使用NSManagedObjectModel將我的數據加載到表視圖,但它沒有工作無法在表格視圖中加載核心數據?

首先,我加載我的KBDataInitializer.m中的所有歌曲,它工作正常。我可以推他們到一個數組。我也可以NSlog所有歌曲的名字。

@implementation KBDataInitializer 

- (NSArray*) getAllSongs{ 

    [self setupDataContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc ] initWithEntityName:@"KBSong"]; 
    NSError *fetchError; 
    NSArray *songs = [self.dataContext executeFetchRequest:fetchRequest error:&fetchError]; 

    if (fetchError !=NULL){ 
     NSLog(@"fetch data ERROR"); 
    } 
     return songs; 
} 

,但是當我打開每首歌曲到的tableview在我的HomeController它什麼都不顯示,當我嘗試NSLog的我的變量*歌曲它顯示的消息(這意味着他們不能加載數據):

$3 = 0x0e483d90 <KBSong: 0xe483d90> (entity: KBSong; id: 0x818b630 <x-coredata://4BA983BA-1914-47C9-A22B-0373E84EAFC8/KBSong/p1> ; data: <fault>) 

However in my viewDidLoad() I can load all songs. 

我在HomeController.h代碼

@interface KBHomeController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
{ 
    NSArray *songs; 
} 
@end 

這是我的表視圖我的代碼加載在HomeController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    KBDataInitializer *data = [[KBDataInitializer alloc]init]; 

    //Use for import updated new Songs 
    //[data importData]; 

    songs = [data getAllSongs]; 

    // This one works fine 
    for (KBSong *song in songs) 
    { 
     NSLog(song.name); 
    } 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    KBSong *song = [songs objectAtIndex:indexPath.row]; 
    cell.textLabel.text = song.name; 
    return cell; 
} 

這是我KBSong.h

@interface KBSong : NSManagedObject 

@property (nonatomic, retain) NSNumber * code; 
@property (nonatomic, retain) NSString * composer; 
@property (nonatomic, retain) NSString * language; 
@property (nonatomic, retain) NSString * lyric; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSNumber * volume; 
@property (nonatomic, retain) NSNumber * favorite; 

@end 
+0

你可以請只嘗試一次NSString * tempString = song.name;然後cell.textlabel.text = tempString;然後告訴我你是否仍然有錯誤。如果這將解決你的問題,那麼我會向你解釋這個 –

+0

我做到了,但結果仍然是一樣的:(。我「po歌」,並且它顯示「...數據:」。奇怪的是,我的歌曲數組仍然有1000個對象,但我無法將每個對象都推送到我的*歌曲變量中。 – tommy

+0

從哪兒來KBSong *歌曲= [歌曲objectAtIndex:indexPath.row]; – RFG

回答

1

基本上你有你所有的內存管理錯誤。

首先,分配一個對象並把它放到一個局部變量將使對象在方法結束後消失。 所以經過您的

- (void)viewDidLoad 

方法您

KBDataInitializer *data = ... 

年底已經超出了範圍試。加上它,

@property (strong, nonatomic) NSManagedObjectContext *dataContext; 

不見了。

其次,歌曲是

@interface KBSong : NSManagedObject 

這使得它們依賴於它們的NSManagedObjectContext。

這些都是基礎知識,來解決當前的問題,短期的辦法是:

KBHomeController.h:

#import <UIKit/UIKit.h> 

@class KBDataInitializer; 

@interface KBHomeController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
{ 
    NSArray *songs; 
} 

@property (nonatomic, strong) KBDataInitializer *data; 

@end 

KBHomeController.m:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.data = [[KBDataInitializer alloc]init]; 

    //Use for import updated new Songs 
    [self.data importData]; 

    songs = [self.data getAllSongs]; 

... 
} 

但這隻能幫助你目前的問題。要了解您做錯了什麼,請閱讀https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html。和http://www.raywenderlich.com/2657/memory-management-tutorial-for-ios。和http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1。加上他們的後續行動。 全部。並試圖理解它。

另請注意我會使用NSFetchedResultsController來完成那項任務。

+0

感謝您的幫助:) – tommy

+0

歡迎,只是接受答案。 ;-) – Christoph