我的变量不断失去价值

时间:2012-01-21 20:46:39

标签: objective-c xcode tableview variable-assignment drilldown

Objective-C和Xcode非常新,并通过修改DrillDownApp教程以适应一个简单的项目,我在这里通过一个Category表向下钻取一个Subject表到一个Quote表然后最后一个Quote 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

1 个答案:

答案 0 :(得分:1)

如果你要更改新创建的viewController的currentLevel,那么你的代码是有意义的。 您可能不希望更改当前viewController的currentLevel变量。

所以例如你的代码应该是这样的:

} else if (currentLevel==0) {
    /* more code */
   rvController.currentLevel = self.currentLevel + 1;
   //Push the new table view on the stack
   [self.navigationController pushViewController:rvController animated:YES];
}

在推送viewController之前分配新的currentLevel很重要。 您在viewDidLoad中检查currentLevel。将viewController推送到导航堆栈时会调用此方法 由于同样的原因,您应该在推送viewController之前分配tableView的dataSource。通常将控制器推到导航堆栈上是最后要做的事情。


由于currentLevel不是类名,因此它应以小写字母开头。 在Objective-C中,只有类(NSString,UIImage,AppDelegate等)应以大写字母开头。


但此代码中存在更多问题。据我所知,推送逻辑完全被打破了。您可以分配因currentLevel不匹配而未使用的变量。

我建议从一个不重复使用相同viewController的应用开始 为每个级别创建一个viewController类。这将清除事情。一旦你完全理解了这个东西,你应该尝试重用部分。