[英]UITableView empty, even after calling reloadData
Kind of following this tutorial here: http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/在此处遵循本教程: http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
I use the fetch records method in my AppNameDelegate.m file.我在我的 AppNameDelegate.m 文件中使用 fetch records 方法。 I then call reload on my tableview.
然后我在我的 tableview 上调用 reload。 Unlike the linked tutorial, I am not using a UITableViewController.
与链接教程不同,我没有使用 UITableViewController。 Instead, I have added a table view to the NavigationController, and connected it to the IBOutlet variable called contactsTable.
相反,我向 NavigationController 添加了一个表视图,并将其连接到名为 contactsTable 的 IBOutlet 变量。
Here's my code for calling and loading the data:这是我调用和加载数据的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:[self.navigationController view]];
[self fetchRecords];
[self.contactsTable reloadData];
[self.window makeKeyAndVisible];
}
The header file: 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
You can find my project source code here as well: https://github.com/agmcleod/SimpleContacts/tree/parttwo In case i left out code you might need.你也可以在这里找到我的项目源代码: https://github.com/agmcleod/SimpleContacts/tree/parttwo如果我遗漏了你可能需要的代码。 Thanks.
谢谢。
The only thing reloadData
does is tell the UITableView
to ask its data source for new data. reloadData
唯一要做的就是告诉UITableView
向其数据源询问新数据。 This means it'll call numberOfSectionsInTableView:
etc. Have you set up this relationship correctly?这意味着它将调用
numberOfSectionsInTableView:
等。您是否正确设置了这种关系?
In your case, your SimpleContactsAppDelegate
instance should be the dataSource
of your table view (do the connection in Interface Builder).在您的情况下,您的
SimpleContactsAppDelegate
实例应该是表视图的dataSource
(在 Interface Builder 中进行连接)。
Consider using NSFetchedResultsController
when using Core Data and UITableViews.在使用 Core Data 和 UITableViews 时考虑使用
NSFetchedResultsController
。 This allows you to not load all the objects into memory at once, etc. Xcode's sample code provides a good starting point for this (I think it's the Navigation Controller based app with Core Data template).这使您不必一次将所有对象加载到 memory 中,等等。Xcode 的示例代码为此提供了一个很好的起点(我认为它是带有核心数据模板的基于导航 Controller 的应用程序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.