Ios UITableView字母顺序有问题

Ios UITableView字母顺序有问题,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有一个包含一些名称的plist文件,我为部分的平铺创建了一个NSMultipleArray,它是字母。我从plist文件中获取名称: - (void)viewDidLoad { NSString *filePath = [[NSBundle mainBundle]pathForResource:@"NameList" ofType:@"plist"] ; namesArray = [NSArray arrayWithContentsOfFile:filePath]; //creat

我有一个包含一些名称的plist文件,我为部分的平铺创建了一个
NSMultipleArray
,它是字母。我从plist文件中获取名称:

- (void)viewDidLoad {
 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"NameList" ofType:@"plist"] ;
    namesArray = [NSArray arrayWithContentsOfFile:filePath];

//create alphabetic letters
    alphabetsSection = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];

}
TableView设置:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return alphabetsSection.count;
}


- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {


    return (NSString*)[alphabetsSection objectAtIndex:section];

}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
    return index;
}

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

    NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [alphabetsSection objectAtIndex:section]]];
    return sectionArray.count;
}


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

    NSString*cellIdentifier = @"cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {

        cell  = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


    //*** I THIK HERE IS THE PROBLEM ****//
    NSString *sectionTitle = [alphabetsSection objectAtIndex:indexPath.section];
    cell.DCName.text = [dinoNamesArray objectAtIndex:indexPath.row];


    return cell;

}

问题是只有字母A的名字才会出现在章节中。我不知道如何将
namesArray
添加到
sectionTitle

=>请参见下面的帖子,这将有助于您进一步澄清:-


这是一个快速修复程序,可以让代码正常工作。在
单元格中,按行索引路径

NSString *sectionTitle = [alphabetsSection objectAtIndex:indexPath.section];
NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", sectionTitle]];
cell.DCName.text = [sectionArray objectAtIndex:indexPath.row];
NSArray *sectionArray = arrayForSection[indexPath.section];
cell.DCName.text = [sectionArray objectAtIndex:indexPath.row];
但这会在显示每一行时过滤数组,这会很慢:最好在第一次加载视图时,对每个部分只过滤一次。大致如下:

- (void)viewDidLoad {
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"NameList" ofType:@"plist"] ;
    namesArray = [NSArray arrayWithContentsOfFile:filePath];

    //create alphabetic letters
    alphabetsSection = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
    // create a corresponding array holding the objects for each section
    arrayForSection = [NSMutableArray array]
    for (NSString *letter in alphabetsSection) {
        NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", letter]];
        [arrayForSection addObject:sectionArray];
    }
}
(您需要为节添加属性
NSMutableArray*arrayForSection

这将构建一个数组,
arrayForSection
,其中的每个元素都是一个数组,其中的对象属于表的相应部分。因此,您可以修改数据源方法以使用此数组:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArray = arrayForSection[section];
    return sectionArray.count;
}
并在
单元格中按行索引路径

NSString *sectionTitle = [alphabetsSection objectAtIndex:indexPath.section];
NSArray *sectionArray = [namesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", sectionTitle]];
cell.DCName.text = [sectionArray objectAtIndex:indexPath.row];
NSArray *sectionArray = arrayForSection[indexPath.section];
cell.DCName.text = [sectionArray objectAtIndex:indexPath.row];

它没有回答我的问题,最好还是评论一下!