user3093525
user3093525

Reputation: 33

UITableCell different array in one section

Hi I have three NSMutableArray. e.g Array - a ,b c. Let say all 3 array have a size of 3.

Would like to ask how do I go about creating 3 section each section contain 3 row (according to no. of array). In each section, the 1st row from a, 2nd row from b, 3rd row from c

1st Section:
[a objectAtIndex:1]
[b objectAtIndex:1]
[c objectAtIndex:1]

2nd Section:
[a objectAtIndex:2]
[b objectAtIndex:2]
[c objectAtIndex:2]

3rd Section:
[a objectAtIndex:3]
[b objectAtIndex:3]
[c objectAtIndex:3]

Thanks!!

Upvotes: 0

Views: 76

Answers (4)

Ashutosh
Ashutosh

Reputation: 2225

You can try a NSmutableDictionary like:

 NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
    [myDict setObject:Array1 forKey:@"Section1"];
    [myDict setObject:Array2 forKey:@"Section2"];
    [myDict setObject:Array3 forKey:@"Section3"];
    NSLog(@"Result %@",myDict);

Now you can use this dictionary to configure cell.

Upvotes: 0

moosa0709
moosa0709

Reputation: 476

Try this code In .h file

#import <UIKit/UIKit.h>


@interface ArrayViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *ivContentsList;
}

// Properties
@property (nonatomic, retain) NSMutableArray *contentsList;

.m file

- (void)viewDidLoad
{   
    [super viewDidLoad];

    NSArray *firstSection = [NSArray arrayWithObjects:@"one", @"two", nil];
    NSArray *secondSection = [NSArray arrayWithObjects:@"three", @"four", @"five", nil];
    NSArray *thirdSection = [NSArray arrayWithObject:@"six"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil];
    [self setContentsList:array];

    [array release], array = nil;


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    NSInteger sections = [[self contentsList] count];
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{

    NSArray *sectionContents = [[self contentsList] objectAtIndex:section];
    NSInteger rows = [sectionContents count];
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSArray *sectionContents = [[self contentsList] objectAtIndex:[indexPath section]];
    NSString *contentForThisRow = [sectionContents objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    [[cell textLabel] setText:contentForThisRow];

    return cell;
}

Upvotes: 0

Julian
Julian

Reputation: 9337

You could put all the arrays (a,b,c) to another array which would act as a container. Select elements for from that container like this: container[rowIndex][sectionIndex]. Please also keep in mind setting numbers for each section:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    //should return number of arrays in container

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    //this should return number of elements corresponding to each section

Those methods are a part of UITableViewDataSource Protocol

Upvotes: 0

santhu
santhu

Reputation: 4792

create another array and push a,b,c arrays into it

NSArray container =@[a,b,c];

in your tableview cellforRow
use container[indexPath.row][indexPath.section]

Upvotes: 2

Related Questions