Isuru
Isuru

Reputation: 31283

populating a grouped UITableView

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.

Upvotes: 1

Views: 4210

Answers (4)

alok srivastava
alok srivastava

Reputation: 761

- (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;
 }

Upvotes: -1

Matej
Matej

Reputation: 9805

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..

Upvotes: 0

iDev
iDev

Reputation: 23278

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.

Upvotes: 5

Jason Silberman
Jason Silberman

Reputation: 2491

Replace

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

With

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

Upvotes: 1

Related Questions