Ios 分区表格视图返回空白表格
我在网上找到了创建带有字母部分的tableview的代码示例,并尝试适应我的tableview 然而,现在模拟器返回一个空白表 我显然错了,有人介意指出错误吗? 我做的每一个改变都没有帮助。 多谢各位Ios 分区表格视图返回空白表格,ios,uitableview,Ios,Uitableview,我在网上找到了创建带有字母部分的tableview的代码示例,并尝试适应我的tableview 然而,现在模拟器返回一个空白表 我显然错了,有人介意指出错误吗? 我做的每一个改变都没有帮助。 多谢各位 #import "RCViewController.h" @interface RCViewController () @end @implementation RCViewController @synthesize content = _content; @synthesize se
#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
代码中有很多东西不是最优的,但导致表为空的原因很可能是您从未填充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];
}
}
}
}
这个内容方法看起来很不可靠。首先,你不希望每次调用它都进行处理,我假设它被调用了不止一次,什么是填充内容?