UITableView - 如何制作部分?

时间:2014-05-01 14:20:59

标签: ios objective-c uitableview sections

如何在UITableView中创建部分?它没有填满所有细胞。

第一部分没问题。但第二部分仅显示1个单元格,第三部分根本没有显示任何单元格。

谢谢。

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

1 个答案:

答案 0 :(得分:0)

  switch (section)
{
    case 0:
        return FileANames.count;
    case 1:
        return FileBNames.count;
    case 2:
        return FileCNames.count;
    default:
        return 0;
}