升级到最新的Xcode后,我的应用程序停止使用数据填充RootViewController。但是,当我点击下一个屏幕并返回时,所有数据都在那里。我无法弄清楚这一点,所以我把代码片段放在下面:
- (void)startLoading
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
self.window.rootViewController = navigationController;
HUD.delegate = self;
[HUD showWhileExecuting:@selector(loadInBackground) onTarget:self withObject:nil animated:YES];
NSLog(@"navigationController = %@", [navigationController view]);
}
- (void)loadInBackground
{
//Copy database to the user's phone if needed.
[self copyDatabaseIfNeeded];
[self populateFromDatabase];
[self performSelectorOnMainThread:@selector(finishedLoading) withObject:nil waitUntilDone:NO];
}
- (void)finishedLoading
{
//back on the main thread now, it's safe to show your view controller
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self startLoading];
}
...
- (void) populateFromDatabase {
// Init the Array
activeCategories = [[NSMutableArray alloc] init];
activeSubjects = [[NSMutableArray alloc] init];
activeQuotes = [[NSMutableArray alloc] init];
categories = [[NSMutableArray alloc] init];
subjects = [[NSMutableArray alloc] init];
quotes = [[NSMutableArray alloc] init];
quoteMaps = [[NSMutableArray alloc] init];
//this is not currently getting the category filled up with anything
[Category getInitialDataToDisplay:[self getDBPath]];
...
@implementation Category
- (id)init {
self = [super init];
if (self) {
subjects = [[NSMutableArray alloc] init];
}
return self;
}
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
NSLog(@"getInitialDataToDisplay is loading data...");
// Use this section to bring in database and populate the array
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
//POPULATE THE QUOTES
FMResultSet *result_quotes = [database executeQuery:@"select * from QUOTE"];
while([result_quotes next]) {
//for some reason q is not instantiated and shows up as nil
Quote *q = [Quote alloc];
q.quote_id = [result_quotes stringForColumn:@"QUOTE_ID"];
q.quote_date = [result_quotes stringForColumn:@"DATE"];
q.title = [result_quotes stringForColumn:@"DESC1"];
q.desc2 = [result_quotes stringForColumn:@"DESC2"];
q.excerpt = [result_quotes stringForColumn:@"EXCERPT"];
q.note = [result_quotes stringForColumn:@"NOTES"];
q.isDirty = NO;
[appDelegate.quotes addObject:q];
// [q release];
}
...
我注意到的一件事是当我打破“Quote * q = [Quote alloc];”线是q是零。
任何有关解决此问题的建议或指示都将受到赞赏。
这是Quote init方法:
#import "Quote.h"
#import "QuotesAppDelegate.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
@implementation Quote
@synthesize quote_id, quote_date, isDirty, title, desc2, excerpt, note;
//- (id) initWithPrimaryKey:(NSInteger) pk {
//
// [super init];
//// quote_id = pk;
// quote_id = [pk stringValueof];
//
// return self;
//}
- (void) addQuote {
// This step will involve both inserting a new record into the quote_map table as well as the quote table.
// Will need the new quote_id first and then will need a new quote_map_id and with these grab the subject_id and insert a new quote_map
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];
// NSNumber *myNum = [NSNumber numberWithInt:self.subject_id];
NSLog(@"Quote addQuote: QUOTE_ID/DESC1/DESC2/EXCERPT = %@ / %@ / %@ / %@", self.quote_id , self.title, self.desc2, self.excerpt);
NSInteger quoteInt = [self.quote_id intValue];
if ([database open]) {
[database executeUpdate:@"insert into QUOTE(quote_id, desc1, desc2, date, excerpt) values(?, ?, ?, ?, ?)",
[NSNumber numberWithInt:quoteInt], self.title, self.desc2, self.quote_date, self.excerpt];
[database close];
}
}
- (void) deleteQuote {
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];
NSInteger quoteIdInt = [self.quote_id intValue];
if ([database open]) {
[database executeUpdate:@"DELETE FROM QUOTE WHERE quote_id = ?",
[NSNumber numberWithInt:quoteIdInt]];
[database executeUpdate:@"DELETE FROM QUOTE_MAP WHERE quote_id = ?",
[NSNumber numberWithInt:quoteIdInt]];
[database close];
}
}
- (NSInteger)getNextQuoteId {
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];
[database open];
FMResultSet *rst = [database executeQuery:@"select max(quote_id) + 1 AS QUOTE_ID from QUOTE"];
[rst next];
NSLog(@"PRINT FIRST QUOTE_ID = %d", [rst intForColumn:@"QUOTE_ID"]);
NSInteger nextId = [rst intForColumnIndex:0];
[database close];
NSLog(@"NEW QUOTE_ID = %ld", (long)nextId);
// NSInteger myInt = [nextSubId intValue];
return nextId;
// [maxSubjectId release];
}
- (void) saveAllData {
if(isDirty) {
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];
if (self.quote_id > 0){
if ([database open]) {
NSLog(@"QUOTE_ID/DESC1/DESC2/EXCERPT = %@ / %@ / %@ / %@", self.quote_id , self.title, self.desc2, self.excerpt);
[database executeUpdate:@"update QUOTE Set desc1 = ?, desc2 = ?, date = ?, excerpt = ? where quote_id = ?",
self.title, self.desc2, self.quote_date, self.excerpt, self.quote_id];
[database close];
}
isDirty = NO;
}
}
}
@end
以下是观点:
@implementation RootViewController
@synthesize categories, qmv, selectedQuote, selectedSubject, subjects, allCategories, allSubjects, mySection, myRow, checkedIndexPath;
@synthesize svc, selectedTabs;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(addItem:)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchItem:)];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self action:@selector(editItem:)];
self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:editButton, searchButton, nil];
//Initialize our table data source
allCategories = [[NSMutableArray alloc] init];
allSubjects = [[NSMutableArray alloc] init];
categories = [[NSMutableArray alloc] init];
subjects = [[NSMutableArray alloc] init];
selectedTabs = [[NSMutableArray alloc] init];
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
self.categories = [appDelegate activeCategories];
self.subjects = [appDelegate activeSubjects];
self.allCategories = [appDelegate categories];
self.allSubjects = [appDelegate subjects];
self.navigationItem.title = @"Categories";
[searchButton release];
[editButton release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// return @"Category List";
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = [self.categories count];
NSLog(@"~~~numberOfRowsInSection: %lu", (unsigned long)count);
return self.categories.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(self.categories.count>0)
{
Category *cat = [self.categories objectAtIndex:indexPath.row];
NSLog(@" category title = %@", cat.category_title);
cell.textLabel.text = cat.category_title;
}
else
{
NSLog(@" ERROR: self.categories has no objects in it!!!!");
}
return cell;
}
另请查看下面的日志。看起来在加载数据时随机调用视图。这可能与视图未显示所有显示的数据的原因有关。
2015-11-07 16:13:23.889 Quotes[11795:757246] getInitialDataToDisplay is loading data...
2015-11-07 16:13:23.896 Quotes[11795:757246] appDelegate.quotes --- 1
2015-11-07 16:13:23.897 Quotes[11795:757246] appDelegate.quotes --- 2
2015-11-07 16:13:23.898 Quotes[11795:757246] appDelegate.quotes --- 3
2015-11-07 16:13:23.898 Quotes[11795:757246] appDelegate.quotes --- 4
2015-11-07 16:13:23.899 Quotes[11795:757246] appDelegate.quotes --- 5
2015-11-07 16:13:23.900 Quotes[11795:757246] appDelegate.quotes --- 6
2015-11-07 16:13:23.900 Quotes[11795:757246] appDelegate.quotes --- 7
2015-11-07 16:13:23.900 Quotes[11795:757246] appDelegate.quotes --- 8
2015-11-07 16:13:23.901 Quotes[11795:757246] appDelegate.quotes --- 9
2015-11-07 16:13:23.901 Quotes[11795:757246] appDelegate.quotes --- 10
2015-11-07 16:13:23.902 Quotes[11795:757246] appDelegate.quotes --- 11
2015-11-07 16:13:23.903 Quotes[11795:757246] appDelegate.quotes --- 12
2015-11-07 16:13:23.903 Quotes[11795:757246] appDelegate.quotes --- 13
2015-11-07 16:13:23.904 Quotes[11795:757246] appDelegate.quotes --- 14
2015-11-07 16:13:23.904 Quotes[11795:757246] appDelegate.quotes --- 15
2015-11-07 16:13:23.906 Quotes[11795:757246] appDelegate.quotes --- 16
2015-11-07 16:13:23.906 Quotes[11795:757246] appDelegate.quotes --- 17
2015-11-07 16:13:23.906 Quotes[11795:757246] appDelegate.quotes --- 18
2015-11-07 16:13:23.907 Quotes[11795:757246] appDelegate.quotes --- 19
2015-11-07 16:13:23.907 Quotes[11795:757246] appDelegate.quotes --- 20
2015-11-07 16:13:23.908 Quotes[11795:757246] appDelegate.quotes --- 21
2015-11-07 16:13:23.908 Quotes[11795:757246] appDelegate.quotes --- 22
2015-11-07 16:13:23.910 Quotes[11795:757246] appDelegate.quotes --- 23
2015-11-07 16:13:23.910 Quotes[11795:757246] appDelegate.quotes --- 24
2015-11-07 16:13:23.911 Quotes[11795:757246] appDelegate.quotes --- 25
2015-11-07 16:13:23.911 Quotes[11795:757246] appDelegate.quotes --- 26
2015-11-07 16:13:23.912 Quotes[11795:757246] appDelegate.quotes --- 27
2015-11-07 16:13:23.912 Quotes[11795:757246] appDelegate.quotes --- 28
2015-11-07 16:13:23.913 Quotes[11795:757246] appDelegate.quotes --- 29
2015-11-07 16:13:23.913 Quotes[11795:757246] appDelegate.quotes --- 30
2015-11-07 16:13:23.913 Quotes[11795:757246] appDelegate.quotes --- 31
2015-11-07 16:13:23.914 Quotes[11795:757246] appDelegate.quotes --- 32
2015-11-07 16:13:23.914 Quotes[11795:757246] appDelegate.quotes --- 33
2015-11-07 16:13:23.915 Quotes[11795:757246] appDelegate.quotes --- 34
2015-11-07 16:13:23.915 Quotes[11795:757246] appDelegate.quotes --- 35
2015-11-07 16:13:23.916 Quotes[11795:757246] appDelegate.quotes --- 36
2015-11-07 16:13:23.916 Quotes[11795:757246] appDelegate.quotes --- 37
2015-11-07 16:13:23.916 Quotes[11795:757246] appDelegate.quotes --- 38
2015-11-07 16:13:23.917 Quotes[11795:757246] appDelegate.quotes --- 39
2015-11-07 16:13:23.917 Quotes[11795:757246] appDelegate.quotes --- 40
2015-11-07 16:13:23.918 Quotes[11795:757246] appDelegate.quotes --- 41
2015-11-07 16:13:23.918 Quotes[11795:757246] appDelegate.quotes --- 42
2015-11-07 16:13:23.918 Quotes[11795:757246] appDelegate.quotes --- 43
2015-11-07 16:13:23.919 Quotes[11795:757246] appDelegate.quotes --- 44
2015-11-07 16:13:23.919 Quotes[11795:757246] appDelegate.quotes --- 45
2015-11-07 16:13:23.920 Quotes[11795:757246] appDelegate.quotes --- 46
2015-11-07 16:13:23.920 Quotes[11795:757246] appDelegate.quotes --- 47
2015-11-07 16:13:23.922 Quotes[11795:757246] appDelegate.quotes --- 48
2015-11-07 16:13:23.922 Quotes[11795:757246] appDelegate.quotes --- 49
2015-11-07 16:13:23.922 Quotes[11795:757246] appDelegate.quotes --- 50
2015-11-07 16:13:23.923 Quotes[11795:757246] appDelegate.quotes --- 51
2015-11-07 16:13:23.923 Quotes[11795:757246] appDelegate.quotes --- 52
2015-11-07 16:13:23.924 Quotes[11795:757246] appDelegate.quotes --- 53
2015-11-07 16:13:23.924 Quotes[11795:757246] appDelegate.quotes --- 54
2015-11-07 16:13:23.924 Quotes[11795:757246] appDelegate.quotes --- 55
2015-11-07 16:13:23.925 Quotes[11795:757246] appDelegate.quotes --- 56
2015-11-07 16:13:23.925 Quotes[11795:757246] appDelegate.quotes --- 57
2015-11-07 16:13:23.925 Quotes[11795:757246] appDelegate.quotes --- 58
2015-11-07 16:13:23.926 Quotes[11795:757246] appDelegate.quotes --- 59
2015-11-07 16:13:23.926 Quotes[11795:757246] appDelegate.quotes --- 60
2015-11-07 16:13:23.927 Quotes[11795:757246] appDelegate.quotes --- 61
2015-11-07 16:13:23.928 Quotes[11795:757246] appDelegate.quotes --- 62
2015-11-07 16:13:23.928 Quotes[11795:757246] appDelegate.quotes --- 63
2015-11-07 16:13:23.928 Quotes[11795:757246] appDelegate.quotes --- 64
2015-11-07 16:13:23.929 Quotes[11795:757246] appDelegate.quotes --- 65
2015-11-07 16:13:23.929 Quotes[11795:757246] appDelegate.quotes --- 66
2015-11-07 16:13:23.929 Quotes[11795:757246] appDelegate.quotes --- 67
2015-11-07 16:13:23.930 Quotes[11795:757246] appDelegate.quotes --- 68
2015-11-07 16:13:23.930 Quotes[11795:757246] appDelegate.quotes --- 69
2015-11-07 16:13:23.931 Quotes[11795:757246] appDelegate.quotes --- 70
2015-11-07 16:13:23.931 Quotes[11795:757246] appDelegate.quotes --- 71
2015-11-07 16:13:23.931 Quotes[11795:757246] appDelegate.quotes --- 72
2015-11-07 16:13:23.932 Quotes[11795:757246] appDelegate.quotes --- 73
2015-11-07 16:13:23.933 Quotes[11795:757246] appDelegate.quotes --- 74
2015-11-07 16:13:23.933 Quotes[11795:757246] appDelegate.quotes --- 75
2015-11-07 16:13:23.933 Quotes[11795:757246] appDelegate.quotes --- 76
2015-11-07 16:13:23.934 Quotes[11795:757246] appDelegate.quotes --- 77
2015-11-07 16:13:23.934 Quotes[11795:757246] appDelegate.quotes --- 78
2015-11-07 16:13:23.935 Quotes[11795:757246] appDelegate.quotes --- 79
2015-11-07 16:13:23.935 Quotes[11795:757246] appDelegate.quotes --- 80
2015-11-07 16:13:23.936 Quotes[11795:757246] appDelegate.quotes --- 81
2015-11-07 16:13:23.936 Quotes[11795:757246] appDelegate.quotes --- 82
2015-11-07 16:13:23.937 Quotes[11795:757246] appDelegate.quotes --- 83
2015-11-07 16:13:23.937 Quotes[11795:757246] appDelegate.quotes --- 84
2015-11-07 16:13:23.937 Quotes[11795:757246] appDelegate.quotes --- 85
2015-11-07 16:13:23.938 Quotes[11795:757246] appDelegate.quotes --- 86
2015-11-07 16:13:23.939 Quotes[11795:757246] appDelegate.quotes --- 87
2015-11-07 16:13:23.939 Quotes[11795:757246] appDelegate.quotes --- 88
2015-11-07 16:13:23.940 Quotes[11795:757246] appDelegate.quotes --- 89
2015-11-07 16:13:23.940 Quotes[11795:757246] appDelegate.quotes --- 90
2015-11-07 16:13:23.940 Quotes[11795:757246] appDelegate.quotes --- 91
2015-11-07 16:13:23.941 Quotes[11795:757246] appDelegate.quotes --- 92
2015-11-07 16:13:23.942 Quotes[11795:757246] appDelegate.quotes --- 93
2015-11-07 16:13:23.942 Quotes[11795:757246] appDelegate.quotes --- 94
2015-11-07 16:13:23.942 Quotes[11795:757246] appDelegate.quotes --- 95
2015-11-07 16:13:23.943 Quotes[11795:757246] appDelegate.quotes --- 96
2015-11-07 16:13:23.943 Quotes[11795:757246] appDelegate.quotes --- 97
2015-11-07 16:13:23.943 Quotes[11795:757246] appDelegate.quotes --- 98
2015-11-07 16:13:23.944 Quotes[11795:757246] appDelegate.quotes --- 99
2015-11-07 16:13:23.944 Quotes[11795:757246] appDelegate.quotes --- 100
2015-11-07 16:13:23.945 Quotes[11795:757246] appDelegate.quotes --- 101
2015-11-07 16:13:23.945 Quotes[11795:757246] appDelegate.quotes --- 102
2015-11-07 16:13:40.838 Quotes[11795:757003] ~~~numberOfRowsInSection: 0
2015-11-07 16:13:40.838 Quotes[11795:757246] appDelegate.quotes --- 103
2015-11-07 16:13:43.003 Quotes[11795:757003] ~~~numberOfRowsInSection: 0
2015-11-07 16:13:43.004 Quotes[11795:757246] appDelegate.quotes --- 104
2015-11-07 16:13:43.971 Quotes[11795:757003] ~~~numberOfRowsInSection: 0
2015-11-07 16:13:43.972 Quotes[11795:757246] appDelegate.quotes --- 105
2015-11-07 16:13:43.973 Quotes[11795:757246] appDelegate.quotes --- 106
2015-11-07 16:13:43.973 Quotes[11795:757246] appDelegate.quotes --- 107
2015-11-07 16:13:43.974 Quotes[11795:757246] appDelegate.quotes --- 108
2015-11-07 16:13:43.974 Quotes[11795:757246] appDelegate.quotes --- 109
2015-11-07 16:13:43.975 Quotes[11795:757246] appDelegate.quotes --- 110
2015-11-07 16:13:43.975 Quotes[11795:757246] appDelegate.quotes --- 111
2015-11-07 16:13:43.977 Quotes[11795:757246] appDelegate.quotes --- 112
2015-11-07 16:13:43.977 Quotes[11795:757246] appDelegate.quotes --- 113
2015-11-07 16:13:43.978 Quotes[11795:757246] appDelegate.quotes --- 114
2015-11-07 16:13:43.979 Quotes[11795:757246] appDelegate.quotes --- 115
2015-11-07 16:13:43.979 Quotes[11795:757246] appDelegate.quotes --- 116
2015-11-07 16:13:43.980 Quotes[11795:757246] appDelegate.quotes --- 117
2015-11-07 16:13:43.980 Quotes[11795:757246] appDelegate.quotes --- 118
2015-11-07 16:13:44.971 Quotes[11795:757003] ~~~numberOfRowsInSection: 0
2015-11-07 16:13:44.972 Quotes[11795:757246] appDelegate.quotes --- 119
2015-11-07 16:13:45.618 Quotes[11795:757003] ~~~numberOfRowsInSection: 0
2015-11-07 16:13:45.619 Quotes[11795:757246] appDelegate.quotes --- 120
2015-11-07 16:13:45.620 Quotes[11795:757246] appDelegate.quotes --- 121
2015-11-07 16:13:45.620 Quotes[11795:757246] appDelegate.quotes --- 122
答案 0 :(得分:1)
您需要覆盖Quote的init方法。
- (instancetype)init {
self = [super init];
if (self) {
// initialize member variables here
_quote_id = nil;
// etc...
}
return self;
}
答案 1 :(得分:1)
看起来您正在尝试在后台创建数据的初始更新,如果您之后立即重新加载UITableView的数据,那么数组中几乎没有任何数据,因为值得到了背景。当你拥有所有的值时,在你的后台线程调用中:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
当您拥有数组中的所有值时,您需要在后台方法的末尾调用它,否则您将不会拥有数组中的所有内容/任何内容,并且它将显示没有数据。你回去后获得数据的原因是因为你在viewWillAppear方法中调用reloadData,那时它必须在后台获得所有值。
编辑:
在你的QuoteDelegate.m文件中,当你完成更新所有数组时,你会想要调用它(userInfoDictionary是你希望对响应通知的方法可用的任何信息的NSDictionary):
[[NSNotificationCenter defaultCenter]postNotificationName:@"YourNotificationName" object:nil userInfo:userInfoDictionary];
在RootViewController.m文件中的viewDidLoad方法中调用:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourCustomMethodToImplementReloadDataIn) name:@"YourNotificationName" object:nil];
然后在你所谓的“yourCustomMethodToImplementReloadDataIn”中,只需调用它,你就可以得到你想要的结果:
[self.tableView reloadData];