选择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包你最喜欢的饮料!

谢谢!

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

-(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];
    } 
}

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

最新更新