分段表视图返回空白表

时间:2014-11-04 13:28:07

标签: ios uitableview

我在网上找到了代码示例,用于创建一个按字母顺序排列的tableview,我试图适应我的tableview。

但是,现在模拟器正在返回一张空白表。

我显然出了问题,有人在指出错误吗? 我所做的每一项改变都没有帮助。 谢谢

#import "RCViewController.h"

@interface RCViewController ()

@end

@implementation RCViewController

@synthesize content = _content;

@synthesize sectionData;
@synthesize sectionNames;


-(NSArray *)content {
NSArray *words = [NSArray arrayWithObjects:@"Tee", @"Club", @"Green", @"Putt", nil];
NSArray *sortedWords = [words     sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSString *currentLetter = @"";

for (NSString *string in sortedWords) {
    if (string.length>0) {
        NSString *letter = [string substringToIndex:1];

        if (![letter isEqualToString:currentLetter]) {
            [sectionNames addObject:letter];
            currentLetter = letter;

            NSMutableArray *oneSection = [NSMutableArray array];
            [sectionData addObject:oneSection];
            [[sectionData lastObject]addObject:string];
        }
    }
}
return _content;
}

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

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


return [[sectionData objectAtIndex:section]count];

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionNames objectAtIndex:section];
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return sectionNames;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"simpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];


    cell.textLabel.text =[[self.sectionData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
}
    return cell;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

有很多东西对你的代码不是最优的,但导致表空白的原因很可能是你从不填充sectionData和sectionName,所以它返回一个没有节名或节数据的表。试试这个:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSArray *words = [NSArray arrayWithObjects:@"Tee", @"Club", @"Green", @"Putt", nil];
NSArray *sortedWords = [words     sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSString *currentLetter = @"";

for (NSString *string in sortedWords) {
    if (string.length>0) {
        NSString *letter = [string substringToIndex:1];

        if (![letter isEqualToString:currentLetter]) {
            [sectionNames addObject:letter];
            currentLetter = letter;

            NSMutableArray *oneSection = [NSMutableArray array];
            [sectionData addObject:oneSection];
            [[sectionData lastObject]addObject:string];
        }
    }
}
}