Object 选择TableView单元格返回不正确的对象

Object 选择TableView单元格返回不正确的对象,object,uitableview,indexing,Object,Uitableview,Indexing,我按照我在网上找到的教程创建了一个表格视图,其中包含一组自定义对象的节和索引。这段代码的一个例外是,当我在表中选择一行时,我将为该部分而不是整个数组创建索引路径。我知道它为什么不起作用,但我不知道如何解决这个问题,这是我的tableview代码单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Cel

我按照我在网上找到的教程创建了一个表格视图,其中包含一组自定义对象的节和索引。这段代码的一个例外是,当我在表中选择一行时,我将为该部分而不是整个数组创建索引路径。我知道它为什么不起作用,但我不知道如何解决这个问题,这是我的tableview代码单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"NameCell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}



// Configure the cell...


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int displayOrder = [defaults integerForKey:@"displayOrder"];
    int sortOrder = [defaults integerForKey:@"sortOrder"];

    NSString *alphabet = [listIndex objectAtIndex:[indexPath section]];

    NSPredicate *sectionPredicate = [[NSPredicate alloc] init];

    if (sortOrder == 1) {  
        //NSLog(@"fName is predicate at cell level");
        sectionPredicate = [NSPredicate predicateWithFormat:@"fName beginswith[c] %@", alphabet];
    } else {
        //NSLog(@"lName is predicate at cell level");
        sectionPredicate = [NSPredicate predicateWithFormat:@"lName beginswith[c] %@", alphabet];
    }
    NSArray *sectionContacts = [filteredList filteredArrayUsingPredicate:sectionPredicate];

if (isSearching) {
    current = [filteredList objectAtIndex:indexPath.row];
} else{
    current = [sectionContacts objectAtIndex:indexPath.row];        
}

if (displayOrder == 1) {
    NSString *fullName = [NSString stringWithFormat:@"%@ %@",[current valueForKey:@"fName"],[current valueForKey:@"lName"]];
    [cell.textLabel setText:fullName];  
    //NSLog(@"FirstNameFirst");

} else {
    NSString *fullName = [NSString stringWithFormat:@"%@ %@",[current valueForKey:@"lName"],[current valueForKey:@"fName"]];
    [cell.textLabel setText:fullName];
    //NSLog(@"LastNameFirst");

}

    [cell.detailTextLabel setText:[current valueForKey:@"extension"]];

return cell;  }
然后我用这个代码调用segue

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"showContact"]) {

    DetailViewController *dvc = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSDictionary *c = [filteredList objectAtIndex:path.row];
    [dvc setCurrentContact:c];   
    [searchBar resignFirstResponder];
} }
问题是objectAtIndex:path.row返回该节的索引,但没有为整个数组修改该索引,因此如果点击该节索引4处的“B”节中的名称,它将返回主数组索引4处的对象。我一直在绞尽脑汁,想知道如何获取完整数组的索引,而不是仅针对该部分的本地数组的索引

如果你能帮忙,我会给你买6包你最喜欢的饮料


谢谢

您的操作方式与第一个函数中的操作方式相同,因此请将您的PrepareForegue更改为:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showContact"]) {
        DetailViewController *dvc = [segue destinationViewController];
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];

        NSDictionary *c;
        NSString *alphabet = [listIndex objectAtIndex:[path section]];
        NSPredicate *sectionPredicate = [[NSPredicate alloc] init];
        if (sortOrder == 1) {  
            sectionPredicate = [NSPredicate predicateWithFormat:@"fName beginswith[c] %@", alphabet];
        } else {
            sectionPredicate = [NSPredicate predicateWithFormat:@"lName beginswith[c] %@", alphabet];
        }
        NSArray *sectionContacts = [filteredList filteredArrayUsingPredicate:sectionPredicate];

        if (isSearching) {
            c = [filteredList objectAtIndex:path.row];
        } else{
            c = [sectionContacts objectAtIndex:path.row];        
        }

        [dvc setCurrentContact:c];   
        [searchBar resignFirstResponder];
    } 
}

请注意,最好是将通用代码提取出来,并创建一个单独的函数,而不是像这样使用它两次。

先生,您让我高兴极了!我对objective C还是相当陌生的,在理解其中一些调用中发送的信息时遇到了困难。我不知道我能够拉取
[listIndex objectAtIndex:[path section]
仍将返回正确的字母。非常感谢你的帮助。