-3

Possible Duplicate:
iPhone UITableView Sections

Basically guys trying two put the table into two different sections but whatever I try it doesn't seem to work, there is my code please can you tell me where I'm going wrong, examples would be greatly appreciated! No one has managed to solve this yet and I've been really stuck for a while. Thanks Sam Houghton.

#import <Foundation/Foundation.h>


@interface FirstLevelViewController : UITableViewController {
    NSArray *controllers;
    NSMutableArray *male;
    NSMutableArray *female;
}

@property (nonatomic, retain) NSArray *controllers;
@property (nonatomic, retain) NSMutableArray *male;
@property (nonatomic, retain) NSMutableArray *female;

@end



#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclosureDetailController.h"
#import "SecondVoiceController.h"
#import "ThirdVoiceController.h"

@implementation FirstLevelViewController
@synthesize controllers,male,female;

-(void)viewDidLoad {
    self.title = @"Voices";
    DisclosureDetailController *th = [DisclosureDetailController alloc];
    th.title = @"First Voice";
    [male addObject:th];
    [th release];

    SecondVoiceController *array2 = [SecondVoiceController alloc];
    array2.title = @"Second Voice";
    [male addObject:array2];
    [array2 release];

    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    array3.title = @"Third Voice";
    [female addObject:array3];
    [array3 release];

    self.controllers = male;
    [female release];
    [male release];
    [super viewDidLoad];
}

-(void)viewDidUnload {
    self.male = nil;
    self.female = nil;
    self.controllers = nil;
    [super viewDidUnload];
}

-(void)dealloc {
    [male release];
    [female release];
    [controllers release];
    [super dealloc];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         switch(section){
                case 0:
                return [male count];
                break;
                case 1:
                return [female count];
                break;
             }
    }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *FirstLevelCell= @"FirstLevelCell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
     }
     SecondLevelViewController *controller;
     switch([indexPath section]){
         case 0:
              controller = [male objectAtIndex: [indexPath row] ];
              break;
         case 1:
              controller = [female objectAtIndex: [indexPath row] ];
              break;
     }
     cell.textLabel.text = controller.title;
     cell.imageView.image = controller.rowImage;
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    SecondLevelViewController *nextViewController = [self.controllers objectAtIndex:row];
    [self.navigationController pushViewController:nextViewController animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

@end

Basically guys trying two put the table into two different sections but whatever I try it doesn't seem to work, there is my code please can you tell me where I'm going wrong, examples would be greatly appreciated! No one has managed to solve this yet and I've been really stuck for a while. Thanks Sam Houghton

4

2 回答 2

2

There are a bunch of things wrong with your code.

  1. Please format your code next time. :)
  2. In several places, you're doing this:

    DisclosureDetailController *th = [DisclosureDetailController alloc];
    ...
    SecondVoiceController *array2 = [SecondVoiceController alloc];
    ...
    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    

    You must never use an object without initializing it. You MUST invoke the init method (or the appropriate initializer) before using it.

  3. Your properties are initialized to nil. You never create the NSMutableArray objects that they're supposed to point to. Thus, [male count] and [female count] will ALWAYS return 0. In other words, simply declaring a property is not the same thing as initializing it.
于 2011-06-22T21:49:23.470 回答
1

很明显,由于格式的原因,代码有点难以阅读,所以如果我看错了什么,我很抱歉。但是您需要在向它们添加任何对象之前调用male = [[NSMutableArray alloc] init];and 。female = [[NSMutableArray alloc] init];如果未分配数组,它们将不会响应addObject. 调用时不会出现任何错误addObject,如果您没有先分配并初始化它,它就不会这样做。

于 2011-06-22T21:28:02.973 回答