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

时间:2012-04-06 02:19:34

标签: object uitableview indexing

我按照我在网上找到的教程创建了一个包含部分和自定义对象数组索引的tableview。此代码的作用是例外,当我在表中选择一行时,该部分的索引路径而不是整个数组。我可以看到为什么它不起作用,但我无法弄清楚如何解决修复,这是我的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包你最喜欢的饮料!

谢谢!

1 个答案:

答案 0 :(得分:0)

你这样做的方式与他们在第一个函数中的方式相同,所以将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];
    } 
}

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