iphone - UITableView 无法显示数据且无法从数组中获取

标签 iphone ios objective-c uitableview nsmutablearray

我是编程新手和 iOS 我正在制作一个 iPhone 应用程序,如 crud 操作和库存系统我无法在 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;
}

最佳答案

有两个问题。

1) 您正在访问name这是 NSEntityDescription 上的属性目的。也许您只是暂时这样做,但如果您创建了 name您的 Category 中的属性(property)实体,那么您必须将该属性名称更改为其他名称(例如 categoryNameitemName ),因为它已被保留。

2) 当您将 NSLog 放入 cellForRowAtIndexPath 时显示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 被优化为充当表格 View 的数据源,因为它只获取屏幕上显示的对象。

此外,您正在使用实例化新表格 View 单元格的旧方法。您应该使用原型(prototype)单元格(在 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;
}

关于iphone - UITableView 无法显示数据且无法从数组中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184365/

相关文章:

ios - 添加 subview 后 UITableViewCell 高度不会更新

objective-c - NSArray 不接受 NSNumber

objective-c - NSUrlSession 代理请求失败,错误代码为 310 (kCFErrorHTTPSProxyConnectionFailure)

ios - UITableViewCell 不尊重 shouldIndentWhileEditing = NO?

ios - 隐藏 View 时如何使用自动布局移动其他 View ?

iphone - 如何裁剪UIImage?

iphone - 防止 uinavigationcontroller 设置中 popViewControllerAnimated 的错误访问崩溃

ios - 所有用户都可以访问和更改的最有效的数据存储方式

iOS 检测 UIImage 内的颜色并裁剪

ios - NSURLConnection不会在多个 View 之间调用完成