Very new to Objective-C and Xcode and working my way through modifying the DrillDownApp tutorial to fit a simple project I have where I drill down through a Category table to a Subject table to a Quote table and then finally a Quote Detail view.

I use the "CurrentLevel" variable to determine which of the above levels I am on however after going through one time through the class, the CurrentLevel seems to get reset to zero. This is after the didSelectRowAtIndexPath method.

I am sure its something super simple and just need a bit of help finding it. I posted the entire class below:

//
//  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

没有正确的解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top