Очень плохо знаком с Objective-C и XCode и проработал мой путь по изменению учебника DrillDownApp, чтобы он соответствовал простому проекту, который у меня есть, где я просматриваю таблицу категорий до таблицы Subject и до таблицы Quote, а затем, наконец, представление Detail Detail. ,
Я использую переменную "CurrentLevel", чтобы определить, на каком из вышеперечисленных уровней я нахожусь, однако после однократного прохождения класса CurrentLevel, похоже, обнуляется. Это после метода didSelectRowAtIndexPath.
Я уверен, что это что-то супер простое, и мне просто нужно немного помочь найти его. Я разместил весь класс ниже:
//
// RootViewController.m
// DrillDownApp
//
// Created by iPhone SDK Articles on 3/8/09.
// Copyright www.iPhoneSDKArticles.com 2009.
//
#import "RootViewController.h"
#import "DrillDownAppAppDelegate.h"
#import "DetailViewController.h"
#import "Subject.h"
#import "Quote.h"
#import "QuoteMap.h"
@implementation RootViewController
@synthesize tableDataSource, CurrentTitle, CurrentLevel;
@synthesize subjects;
@synthesize quotes;
@synthesize quoteMap;
@synthesize categories;
@synthesize subject_id;
@synthesize subject;
@synthesize category;
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
if(CurrentLevel == 0) {
//Initialize our table data source
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.tableDataSource = tempArray;
[tempArray release];
// create array that will store the data
DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [appDelegate categories];
self.subjects = [appDelegate subjects];
self.quotes = [appDelegate quotes];
self.quoteMap = [appDelegate quoteMap];
NSLog(@" TableDataSource Count: %i", self.tableDataSource.count);
self.navigationItem.title = @"Category";
}
else if (CurrentLevel==1) {
self.navigationItem.title = @"Subject";
self.title = @"Subject";
}else if (CurrentLevel==2){
self.navigationItem.title = @"Quote";
self.title = @"Quote";
}else{
self.navigationItem.title = @"Detail";
self.title = @"Detail";
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(CurrentLevel==0){
NSLog(@"////// CURRENT LEVEL = 0 LOADING CATEGORIES INTO TABLE...");
if(self.tableDataSource.count>0){
Category *cat = [self.tableDataSource objectAtIndex:indexPath.row];
NSLog(@" category title = %@", cat.title);
cell.textLabel.text = cat.title;
} else {
NSLog(@" ERROR: self.tableDataSource has no objects in it!!!!");
}
}
else if(CurrentLevel==1){
NSLog(@"////// CURRENT LEVEL = 1 LOADING SUBJECTS INTO TABLE...");
if(self.tableDataSource.count>0){
Subject *sub = [self.tableDataSource objectAtIndex:indexPath.row];
NSLog(@" subject title = %@", sub.title);
cell.textLabel.text = sub.title;
}
}
else if(CurrentLevel==2){
NSLog(@"////// CURRENT LEVEL = 2 LOADING QUOTES INTO TABLE...");
if(self.tableDataSource.count>0){
Quote *q = [self.quotes objectAtIndex:indexPath.row];
NSLog(@" Quote title = %@", q.title);
cell.textLabel.text = q.title;
}
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(CurrentLevel==0){
DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSUInteger count = [appDelegate.categories count];
NSLog(@"Count of records in categories array: %i", count);
return appDelegate.categories.count;
}else if(CurrentLevel==1){
NSUInteger count = [self.tableDataSource count];
NSLog(@"Count of records in categories array: %i", count);
return self.tableDataSource.count;
}else if(CurrentLevel==2){
NSUInteger count = [self.quotes count];
NSLog(@"Count of records in quotes array: %i", count);
return self.quotes.count;
}else{
return 1;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
#pragma mark Table view methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Prepare to tableview.
RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
NSLog(@"self.CurrentLevel= %i", self.CurrentLevel);
if (CurrentLevel==2){
//Set the title;
rvController.CurrentTitle = @"Quote";
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
// rvController.tableDataSource = tempArray;
//Increment the Current View
CurrentLevel = CurrentLevel + 1;
NSLog(@" CurrentLevel = %i", CurrentLevel);
[rvController release];
} else if (CurrentLevel==1) {
DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
self.quotes = [appDelegate quotes];
self.quoteMap = [appDelegate quoteMap];
//Get the selected data source.
Subject *cat = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSLog(@"subject_id selected was: %@", cat.subject_id);
//get the quote_ids from quote_map for this subject_id
NSPredicate *filterSubjectId = [NSPredicate predicateWithFormat:@"subject_id == %@", cat.subject_id];
NSArray *quoteMapSection = [self.quoteMap filteredArrayUsingPredicate:filterSubjectId];
NSLog(@"Count of quoteMapSections = %i", quoteMapSection.count);
//loop through quote array and create a new array with matching quote_ids
// TAKE THE QUOTE_ID AND USE INDEXOFOBJECT METHOD TO GRAB QUOTE OBJECT, THEN ADD IT TO THE NEW ARRAY
QuoteMap *q = [quoteMapSection objectAtIndex:0];
NSLog(@" FIRST QUOTEMAP: %@", q.quote_id);
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (QuoteMap *qm in quoteMapSection){
NSLog(@"quote_id=%@",qm.quote_id );
//use predicate to filter out just the one I want
NSPredicate *f = [NSPredicate predicateWithFormat:@"quote_id == %@", qm.quote_id];
NSArray *quoteFiltered = [self.quotes filteredArrayUsingPredicate:f];
Quote *qq = [quoteFiltered objectAtIndex:0];
[tempArray addObject:qq];
[qq release];
}
NSLog(@"Count of tempArray == %i", tempArray.count);
//Set the title;
rvController.CurrentTitle = cat.title;
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
NSLog(@"QuoteMaps were loaded into tempArray in didSelectRowAtIndexPath method....... ");
rvController.tableDataSource = tempArray;
//Increment the Current View
CurrentLevel = CurrentLevel + 1;
NSLog(@" CurrentLevel = %i", CurrentLevel);
[rvController release];
} else if(CurrentLevel==0){
//Get the dictionary of the selected data source.
Category *cat = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSLog(@"category selected was: %@", cat.title);
NSLog(@"Count of subjects = %i", subjects.count);
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"category_title contains[cd] %@", cat.title];
NSArray *subs = [subjects filteredArrayUsingPredicate:bPredicate];
NSLog(@"Count of subs = %i", subs.count);
//Set the title;
rvController.CurrentTitle = @"Subjects";
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = subs;
//Increment the Current View
CurrentLevel = CurrentLevel + 1;
NSLog(@" CurrentLevel = %i", CurrentLevel);
[rvController release];
} else {
//IF ITS THE DETAIL LEVEL YET
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
}
- (void)dealloc {
[CurrentTitle release];
[tableDataSource release];
[super dealloc];
}
@end