2013-10-04 60 views
2

獲取我新的編程和iOS我想提出類似的CRUD操作和庫存系統的iPhone應用程序我不能夠顯示數據,並在UITableView的的UITableView不能夠顯示數據,而不是從陣列

獲取數據

但當打印數組時,則產生這種類型的輸出。

2013-10-03 15:45:04.228 inventrymanagementsystem[1968:c07] name: (
    "<Category: 0x7453f40> (entity: Category; id: 0x744a3b0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p1> ; data: <fault>)", 
    "<Category: 0x7456500> (entity: Category; id: 0x74501f0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p2> ; data: <fault>)", 
    "<Category: 0x7456210> (entity: Category; id: 0x7450970 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p3> ; data: <fault>)", 
    "<Category: 0x7457830> (entity: Category; id: 0x743bd90 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p4> ; data: <fault>)", 
    "<Category: 0x7455cf0> (entity: Category; id: 0x7438670 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p5> ; data: <fault>)", 
    "<Category: 0x744b060> (entity: Category; id: 0x7438530 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p6> ; data: <fault>)", 
    "<Category: 0x74496f0> (entity: Category; id: 0x7436050 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p7> ; data: <fault>)", 
    "<Category: 0x744f290> (entity: Category; id: 0x744db90 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p8> ; data: <fault>)", 
    "<Category: 0x744eaa0> (entity: Category; id: 0x7439d40 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p9> ; data: <fault>)", 
    "<Category: 0x744fec0> (entity: Category; id: 0x74385c0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p10> ; data: <fault>)", 
    "<Category: 0x744f410> (entity: Category; id: 0x7445de0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p11> ; data: <fault>)", 
    "<Category: 0x74557c0> (entity: Category; id: 0x744ebe0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p12> ; data: <fault>)", 
    "<Category: 0x7456190> (entity: Category; id: 0x7450b30 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p13> ; data: <fault>)", 
    "<Category: 0x7457010> (entity: Category; id: 0x7453180 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p14> ; data: <fault>)", 
    "<Category: 0x744b740> (entity: Category; id: 0x74524b0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p15> ; data: <fault>)" 
) 

我不明白是什麼問題。

代碼

#import "IMScategoriesViewController.h" 
#import "IMSAddCategoryViewController.h" 
#import "IMSAppDelegate.h" 
#import "Category.h" 

@interface IMScategoriesViewController() 
@property (strong) NSMutableArray *categories; 
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic,retain) NSFetchedResultsController *fetchedresultcontroller; 
@end 
@implementation IMScategoriesViewController 
- (NSManagedObjectContext *)managedObjectContext 
{ 
    NSManagedObjectContext *context; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    if ([delegate performSelector:@selector(managedObjectContext)]) { 
     context = [delegate managedObjectContext]; 
    } 
    return context; 
} 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
//- (void)viewDidAppear:(BOOL)animated 
//{ 
// [super viewDidAppear:animated]; 

    - (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Fetch the devices from persistent data store 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Category"]; 
    self.categories = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 
    NSLog(@"name: %@",self.categories); 
    [self.tableView reloadData]; 

// IMSAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 
// self.managedObjectContext = appDelegate.managedObjectContext; 
// 
// // Fetching Records and saving it in "fetchedRecordsArray" object 
// self.categories = [[appDelegate getallcategories]mutableCopy]; 
// NSLog(@"name: %@",self.categories); 
// [self.tableView reloadData]; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.getallcategories count]; 
} 

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

    //add this 
    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    Category * category = [self.getallcategories objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [category name]; 
    return cell; 
} 
+0

多少行顯示了視圖加載時?在你的'cellForRowAtIndexPath'方法中,當你把NSLog(@「這個類別是:%@」,category)時會發生什麼;'' – iwasrobbed

+0

順便說一句,你應該使用'NSFetchedResultsController'而不是獲取整個'Categories'集合,因爲它只提取需要在屏幕上顯示的內容。 – iwasrobbed

+0

Put'NSLog(@「This category is:%@」,category);'的,這是在節目輸出2013年10月4日08:31:16.062名:( 「<類別:0x845c8d0>(實體:類別; ID:0x845b720 ;數據:) 「 」<類別:0x845cb80>(實體:類別; ID:0x845b730 ;數據:)「, 」(entity:Category; id:0x845b740 ; data:)「, –

回答

0

有兩個問題。

1)您正在訪問name,這是NSEntityDescription對象上的一個屬性。也許您只是暫時這樣做,但如果您在Category實體中創建了name媒體資源,那麼您必須將該媒體資源名稱更改爲其他內容(例如categoryNameitemName),因爲它已被保留。

2)當您在cellForRowAtIndexPath方法中放入NSLog以顯示category時,它返回了一個類別數組,因此顯然不能正確訪問數據源。

的問題是,你使用[self.getallcategories objectAtIndex:indexPath.row];,而不是訪問您在viewDidLoad方法創建的原始陣列應改爲:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Access the proper array here that you loaded in viewDidLoad 
    return [self.categories count]; 
} 

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

    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 

    // Access the proper array here that you loaded in viewDidLoad 
    Category * category = [self.categories objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [category name]; 
    return cell; 
} 

正如我所提到的,應該使用NSFetchedResultsController代替執行一個類似於tableview的提取。 NSFetchedResultsController經過優化,充當表視圖的數據源,因爲它只獲取屏幕上顯示的對象。

另外,您正在使用實例化新表格視圖單元格的舊方法。您應該使用的原型細胞(設置在Interface Builder的小區標識),然後你的代碼應該是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Make sure your prototype cell in Interface Builder has an identifier called "Cell" 
    UITableViewCell *cell = [UITableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    // Access the proper array here that you loaded in viewDidLoad 
    Category *category = [self.categories objectAtIndex:indexPath.row]; 

    // Assuming the Category entity has somePropertyNameHere method 
    cell.textLabel.text = category.somePropertyNameHere; 
    return cell; 
} 
+0

問題沒有解決 –

0

請檢查您的委託和數據源連接。設置你的表格視圖委託和數據源。 @interface myViewController:UIViewController {...}設置它。並參考表格視圖教程

+0

我檢查了代理和數據源的連接。連接沒問題 –