user2966615
user2966615

Reputation: 383

A uitable view with 3 groups in it

i am a developing a ios app with a view controller and a table view in it. i am trying to load list of items in 3 groups but when i compile it it shows correct count but not showing all the items jus repeating items. please help. let me post my code here.

@interface ViewController ()

@end

@implementation ViewController {
    NSArray *menuItems;
    NSArray *menuItems2;
    NSArray *dash;

}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
    self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
    menuItems = @[@"itm1", @"itm2", @"itm3", @"itm4"];
    menuItems2 = @[@"itm1", @"itm2", @"itm3", @"itm4"];
    dash = @[@"itm1", @"itm2"];
}
#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (section == 0) {
        return [menuItems count];
    }
    if (section == 1) {
        return [menuItems2 count];
    }
    if (section == 2) {
        return [dash count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    return cell;
}

@end

Upvotes: 0

Views: 32

Answers (2)

DrummerB
DrummerB

Reputation: 40211

You don't configure your cells. After dequeuing a cell, you have to set its title.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Get the correct array, depending on the current section.
    NSArray *items = nil;
    if (indexPath.section == 0) {
        items = menuItems;
    }
    else if (indexPath.section == 1) {
        items = menuItems2;
    }
    else if (indexPath.section == 2) {
        items = dash;
    }

    // Get the string from the array and set the cell's title
    NSString *title = items[indexPath.row];
    cell.textLabel.text = title;

    return cell;
}

Upvotes: 2

rmaddy
rmaddy

Reputation: 318804

Your cellForRowAtIndexPath... method needs to be written so the cell is populated with proper data based on the cell's section and row. Right now you don't do anything but use an empty cell.

Upvotes: 3

Related Questions