2012-04-06 60 views
0

我遵循我在線發現的教程來創建帶有自定義對象數組的部分和索引的tableview。此代碼適用於當我在表中選擇一行時,該部分的索引路徑而不是整個數組的情況。我可以看到它爲什麼不起作用,但我無法弄清楚如何解決這個問題,這是我用於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返回索引該節,但它不被修改爲整個陣列,因此,如果「B」部分是在索引4在名稱該部分被輕敲,它返回主數組索引4處的對象。我一直在摸索如何獲得完整數組的索引,而不是那些僅在本節中使用的索引。

如果可以幫忙的話,我會給你買6包你喜歡的飲料!

謝謝!

回答

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

注意,它很可能是最好的可以拉出公共代碼,並單獨函數而不是像這樣使用它兩次。

+0

你先生,讓我的一天!對於客觀的C,我仍然很陌生,並且一直無法理解這些調用中發送的信息。我不知道我可以拉''listIndex objectAtIndex:[path section]];'仍然會返回正確的字母。非常感謝你的幫助。 – Tenthrow 2012-04-06 11:29:24

相關問題