[英]UITableView empty, even after calling reloadData
在此处遵循本教程: http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
我在我的 AppNameDelegate.m 文件中使用 fetch records 方法。 然后我在我的 tableview 上调用 reload。 与链接教程不同,我没有使用 UITableViewController。 相反,我向 NavigationController 添加了一个表视图,并将其连接到名为 contactsTable 的 IBOutlet 变量。
这是我调用和加载数据的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:[self.navigationController view]];
[self fetchRecords];
[self.contactsTable reloadData];
[self.window makeKeyAndVisible];
}
header 文件:
//
// SimpleContactsAppDelegate.h
// SimpleContacts
//
// Created by Aaron McLeod on 11-05-28.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface SimpleContactsAppDelegate : NSObject <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UINavigationController *navigationController;
// view for adding new contacts
UIViewController *newContactView;
UIButton *addButton;
// controls for the addContactView
UIButton *saveContactButton;
UITextField *nameField;
UITextField *emailField;
UITextField *phoneField;
UITableView *contactsTable;
NSMutableArray *contactArray;
}
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIButton *addButton;
@property (nonatomic, retain) IBOutlet UITableView *contactsTable;
@property (nonatomic, retain) NSMutableArray *contactArray;
// controller and fields for the form
@property (nonatomic, retain) IBOutlet UIViewController *newContactView;
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *emailField;
@property (nonatomic, retain) IBOutlet UITextField *phoneField;
@property (nonatomic, retain) IBOutlet UIButton *saveContactButton;
- (NSString *)applicationDocumentsDirectory;
- (IBAction) saveContact;
- (IBAction) switchToAddContactView;
- (void)fetchRecords;
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
@end
你也可以在这里找到我的项目源代码: https://github.com/agmcleod/SimpleContacts/tree/parttwo如果我遗漏了你可能需要的代码。 谢谢。
reloadData
唯一要做的就是告诉UITableView
向其数据源询问新数据。 这意味着它将调用numberOfSectionsInTableView:
等。您是否正确设置了这种关系?
在您的情况下,您的SimpleContactsAppDelegate
实例应该是表视图的dataSource
(在 Interface Builder 中进行连接)。
在使用 Core Data 和 UITableViews 时考虑使用NSFetchedResultsController
。 这使您不必一次将所有对象加载到 memory 中,等等。Xcode 的示例代码为此提供了一个很好的起点(我认为它是带有核心数据模板的基于导航 Controller 的应用程序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.