1

I'm trying to populate a grouped UITableView using an NSMutableArray. I want each element in the array to have its own section. i.e : One element (one row) per one section.

This is my code I have written so far.

#import "MailViewController.h"

@interface MailViewController ()

@end

@implementation MailViewController

NSMutableArray *mailboxes;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    mailboxes = [[NSMutableArray alloc] initWithObjects:@"Inbox", @"Drafts", @"Sent Items", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return mailboxes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];

    return cell;
}

@end

Currently this is how it looks.

enter image description here

How can I get this the way I described above? (first section: Inbox, second section: Drafts, Third section: Sent Items etc.) I've gotten close but not quite there yet.

Thank you.

1
  • The above screenshot depicts, you want row not sections, are you talking about sections? Because sections do have their own delegate methods for defining their titles. Mar 8, 2013 at 7:15

4 Answers 4

5

You should change:

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];

to

cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];

These lines should go in the cellForRowAtIndexPath: method.

Your array index should be based on the sections and not the rows. Row is always fixed at zero.

1

Replace

    cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];

With

    cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
0

If you want section headings as well implement the - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section and return a string based on the section..

With the code from other answers, you will be able to display one cell inside each section. What I prefer to do is have a multi-dimensional NSArray of pre-built cells and when I need them they're in index [section][row].

You currently have one array with just sections and trying to access them as rows (as other answers suggest). Your code won't work if you have more mailboxes in one table group..

-1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
    return [mailboxes count];
 }

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
  {
   return 1;
  }

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
    }
      switch(indexpath.section){
             case 0:
              cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
              case 1:
               cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
               case 2:
               cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
                default:
                 break;
             }
   return cell;
 }
1
  • there is no need to use a switch statement here. Dec 22, 2013 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.