-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

Community
  • 1
  • 1
Sam
  • 83
  • 1
  • 1
  • 6
  • Not exactly sure what you are trying to do, but this definitely looks wrong: DisclosureDetailController *th = [DisclosureDetailController alloc]; You should have [[DisclosureDetailController alloc] init]; – hundreth Jun 22 '11 at 21:26
  • 1
    Can you update your post so that it is using a code block? – Rob Jun 22 '11 at 21:27
  • Well I'm trying to create a table view that has three cells that all push a new view I need the first two cells to be in a sections called male on the table and the third voice two be in another section female on the table and i am not sure how to split my three cells into different sections thanks Sam Houghton – Sam Jun 22 '11 at 21:28
  • 2
    Pasting your code (without formatting!) and saying "it's not working" is not a good way to request for help. – Dave DeLong Jun 22 '11 at 21:42

2 Answers2

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.
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

The code is a little tough to read because of the formatting, obviously, so I'm sorry if I misread anything. But you need to call male = [[NSMutableArray alloc] init]; and female = [[NSMutableArray alloc] init]; before you add any objects to them. If the arrays are not allocated, they will not respond to addObject. You won't get any kind of error when calling addObject, it just won't do it if you haven't first alloc'd and init'd it.

Mike C
  • 1,959
  • 2
  • 17
  • 17