Question

How do I make sections in UITableView? It is not filling in all the cells.

The first section is ok. But the second section is only showing 1 cell and the third section is not showing any cell at all.

Thank you.

//  ViewController.m
//  LoadingPlist

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController {

NSDictionary *FileADetails;
NSArray *FileANames;

NSDictionary *FileBDetails;
NSArray *FileBNames;

NSDictionary *FileCDetails;
NSArray *FileCNames;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}

//set the header title. Display correctly.

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:   (NSInteger)section {   
if (section == 0) {
    return @"File A Details Title";
} else if (section == 1) {
    return @"File B Details Title";
} else if (section == 2) {
    return @"File C Details Title";
}
return 0;
}



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

// I am Not sure if this logic is sound....???

if (section == 0) {
    return FileADetails.count;
}
else if (section == 1) {
    return FileBDetails.count;        
}
else if (section == 2) {
    return FileCDetails.count;    
}
return 0;
}


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

//create cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//retrieve an image

UIImage *myImage = [UIImage imageNamed:@"DemoCellImage"];
[cell.imageView setImage:myImage];


// Problem is here – it does not fill in the cells for FileB (just one cell) and FileC cells are missing.

if (indexPath.section == 0) {
    cell.textLabel.text = FileANames[indexPath.row];
    cell.detailTextLabel.text = FileADetails[FileANames[indexPath.row]];
}

else if (indexPath.section == 1) {
    cell.textLabel.text = FileBNames[indexPath.row];
    cell.detailTextLabel.text = FileBDetails[FIleBNames[indexPath.row]];
}

else if (indexPath.section == 2) {
    cell.textLabel.text = FileCNames[indexPath.row];
    cell.detailTextLabel.text = FileCDetails[FileCNames[indexPath.row]];        
}

//fill cell
return cell;
}


- (void)viewDidLoad
{
[super viewDidLoad];


NSURL *urFileA = [[NSBundle mainBundle] URLForResource:@"File_A" withExtension:@"plist"];

FileADetails = [NSDictionary dictionaryWithContentsOfURL:urlFileA];
FileANames = FileADetails.allKeys;    


NSURL *urlFileB = [[NSBundle mainBundle] URLForResource:@"File_B withExtension:@"plist"];

FileBDetails = [NSDictionary dictionaryWithContentsOfURL:urlFileB];
FileBNames = FileBDetails.allKeys;


NSURL *urlFileC = [[NSBundle mainBundle] URLForResource:@"File_C" withExtension:@"plist"];

FileCDetails = [NSDictionary dictionaryWithContentsOfURL:urlFileC];
FileCNames = FileCDetails.allKeys;
}


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

@end
Was it helpful?

Solution

  switch (section)
{
    case 0:
        return FileANames.count;
    case 1:
        return FileBNames.count;
    case 2:
        return FileCNames.count;
    default:
        return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top