我是编程和iOS的新手我正在制作像crud操作和库存系统这样的iPhone应用程序我无法在UITableView中显示数据和获取数据
但是当Print数组产生这种类型的输出时。
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 :(得分:0)
请检查您的代理和数据源连接。设置表视图委托和数据源。 @interface myViewController:UIViewController {...}设置它。并参考表格视图教程
答案 1 :(得分:0)
有两个问题。
1)您正在访问name
对象上的name
属性。也许您只是暂时这样做,但如果您在Category
实体中创建了categoryName
属性,那么您必须将该属性名称更改为其他名称(例如itemName
或{ {1}})因为这已经被保留了。
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;
}
正如我所提到的,你应该使用NSEntityDescription而不是为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;
}