2012-04-06 10 views
0

私はオンラインで見つかったチュートリアルで、カスタムオブジェクトの配列からセクションとインデックスを持つテーブルビューを作成しました。このコードは、テーブル内の行を選択すると、そのセクションのインデックスパスであり、配列全体ではありません。私はなぜそれが動作しませんが、私は修正プログラムに対処する方法を把握することはできません見ることができる、これは私のテーブルビューコードのセルです。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パック買うよ!

ありがとうございます!

答えて

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

注意を関数を2回使用する代わりに、この関数を使用します。

+0

あなた、私の日を作った!私はまだ客観的なCにはかなり新しく、これらの呼び出しの中でどの情報が送られるかを理解するのに困っています。私は '[listIndex objectAtIndex:[path section]];を引っ張っても正しい文字を返すことができるかどうかわかりませんでした。手伝ってくれてどうもありがとう。 – Tenthrow

関連する問題