8

I have been programming a UITableView and each cells pushes a new view, All I Want to do is add two new sections one male and one female, first and second voice need to be in the male section and the third voice needs to be in the female section.

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



@implementation FirstLevelViewController
@synthesize controllers;

-(void)viewDidLoad {
    self.title = @"Voices";

    NSMutableArray *male = [[NSMutableArray alloc] init];


    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";
    [male addObject:array3];
    [array3 release];




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

-(void)viewDidUnload {

    self.controllers = nil;
    [super viewDidUnload];
}

-(void)dealloc {

    [controllers release];
    [super dealloc];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.controllers count];
}

-(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];
    }
    NSUInteger row = [indexPath row];
    SecondLevelViewController *controller = [controllers objectAtIndex:row];
    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];
}

All I Want to do is add two new sections one male and one female, first and second voice need to be in the male section and the third voice needs to be in the female section. Please help been stuck on this for a while!

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
Sam
  • 83
  • 1
  • 1
  • 6

2 Answers2

24

The tableView delegate has a method called numberOfSectionsInTableView. Return the number of sections that you want to create.

Then, in the cellForRowAtIndexPath use indexPath's other property [indexPath section] to segregate rows based on sections.

An example

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2; //one male and other female
}

-(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;
}
Mridul Kashatria
  • 4,157
  • 2
  • 18
  • 15
  • hey thanks for the feedback but please could you go into a bit more detail would really appreciate some written code. Thanks, Sam Houghton. – Sam Jun 22 '11 at 20:05
  • 2
    apple goes into detail: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html – vikingosegundo Jun 22 '11 at 20:13
  • It gave me lots of errors and said male and female were undeclared identifier – Sam Jun 22 '11 at 20:37
  • That is just an example, I wrote it without actually compiling it. male and female should be NSMutable arrays like controllers. If third voice needs to be female, it is better to add it to a array named female. – Mridul Kashatria Jun 22 '11 at 20:40
  • and my static NSString FirstLevelCell has also stopped working. – Sam Jun 22 '11 at 20:41
0

Hows this?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return # of rows in section;
            break;
        case 1:
            return # of rows in section;
break;
    }
    return 0;
}
James Dunay
  • 2,714
  • 8
  • 41
  • 66
  • Thanks for the reply but that didn't work it just put the first and second voice in the table and the third voice went, and there were still no sections, thanks Sam Houghton – Sam Jun 22 '11 at 20:23