UITableView multiple section with different array setup

15,844

Solution 1

I think Array of Arrays section in following document will guide you: http://briterideas.blogspot.com/2012/07/uitableview-how-to-part-3-multiple.html

Solution 2

Inside cellForRowAtIndexPath function, put a condition like this.

if(indexpath.section == 0)
{
   cell.textlabel.text = [array1 objectAtIndex:indexpath.row];
}
else if(indexpath.section == 1)
{
   cell.textlabel.text = [array2 objectAtIndex:indexpath.row];
}
..
..
..
etc
Share:
15,844
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to have 7 sections with different row in uitableview, I don't know how should I write my different array and set it in different sections, I write it for one but I have same array in each section!

    would you please help me!

    Thanks in advance!

    I mean like a below picture but with the different array : for example I don't want to have test and hey in each sections

    here is my code:

    @synthesize monthTitle;
    
    - (id)initWithStyle:(UITableViewStyle)style
    {
    self = [super init];
    if (self) {
        monthTitle = [[NSMutableArray alloc] init];
    }
    return self;
    
    }
    
    - (void)didReceiveMemoryWarning
    {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
    [super viewDidLoad];
     arry = [[NSMutableArray alloc] init];
    [arry addObject:@"test"];
    [arry addObject:@"hey"];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
     target:self action:@selector(addNewItem)];
    self.navigationItem.rightBarButtonItem = rightButton;
    }
    
    -(void)addNewItem{
    [arry addObject:@"New Day"];
        [self.tableView reloadData];
    }
    //- (IBAction)DeleteButtonAction:(id)sender
    //
    //{
    //    
    //    [arry removeLastObject];
    //    
    //    [self.tableView reloadData];
    //    
    //}
    
    - (void)viewDidUnload
    {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
    [super viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    // Return the number of sections.
    return 7;
    //_week.days.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    // Return the number of rows in the section.
    return [arry count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [self.tableView 
                             dequeueReusableCellWithIdentifier:@"Cell"];
    
    
    cell.textLabel.text = [arry objectAtIndex:indexPath.row];
    return cell;
    
    }
    
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if(section == 0)
        return @"Monday";
    else if(section == 1){
        return @"Tuesday";
    }else if(section == 2){
        return @"Wednesday";
    } else if(section == 3){
        return @"Thuesday";
    } else if(section == 4){
        return @"Friday";
    } else if(section == 5){
        return @"Saturday";
    }else
        return @"Sunday";
    
    }
    
    
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Return NO if you do not want the specified item to be editable.
    return YES;
    }
    
    
    
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
      forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (editingStyle == UITableViewCellEditingStyleDelete)  
    {
        [arry removeObjectAtIndex:indexPath.row];
        //[self.monthTitle removeObjectAtIndex:indexPath.row];
    
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:
         UITableViewRowAnimationFade];
    
    //    }else if (editingStyle==UITableViewCellEditingStyleInsert) {
    
    } 
    

    // [self.tableView reloadData]; }

    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib
      name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    }
    
    @end