0

SQLite データベースによって作成された UITableView があります。今日、sectionIndexTitlesForTableView デリゲート メソッドを使用してセクション ベースのグループ化を追加しました。現在、セルが選択されている場合、indexPath.row の文字列は、選択したセルのテキストと同じではありません。

私のコードはこのように機能します。

  1. SQLite データベースからビジネスを保持する配列を作成します。
  2. その配列をアルファベット順に並べ替えます。
  3. データベース内のビジネスが始まるアルファベットの文字のみを使用して、アルファベットの文字の配列を作成します。
  4. この配列を NSPredicate と共に使用して、ビジネスを最初の文字でアルファベット順にグループ化するグループ化ヘッダー ビューを提供します。
  5. 選択された行が NSUserDefaults ファイルに書き込まれ、View Controller がプッシュされる (iPhone) か、そのキーに Observer が追加される (iPad)。

残念ながら、ヘッダー ビューを追加したため、indexPath.row は、選択したセルの TextLabel の文字列とはまったく異なる文字列を返すようになり、別のビジネス情報が表示されます。

メイン配列の重要なコード ブロックは次のとおりです。

- (void)viewDidLoad
{
 // Lots of code...
 arrayName = [[NSMutableArray alloc] init];
 NameSet = [[NSMutableSet alloc] init];
 sortedArray = [arrayName sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

 alphabet = [[NSMutableArray alloc] init];
 [alphabet addObject:@"{search}"];
 for (int i=0; i<[sortedArray count]-1; i++)
 {
  char alphabetUni = [[sortedArray objectAtIndex:i] characterAtIndex:0];
  NSString *uniChar = [NSString stringwithFormat:@"%c", alphabetUni];
  if (![alphabet containsObject:uniChar])
  {
    [alphabet addObject:uniChar];
  }
 }
 }

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
  return [alphabet count];
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 NSInteger rows = 0;

 NSString *alpha = [alphabet objectAtIndex:section];

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
 businesses = [sortedArray filteredArrayUsingPredicate:predicate];

 if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    rows = [self.searchResults count];
 }
 else {
    rows = [businesses count];
 }

  return rows;
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection (NSInteger)section
 {
  return [alphabet objectAtIndex:section];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSString *CellIdentifier = @"Cell";
  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:CellIdentifier];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  }

  NSString *alpha = [alphabet objectAtIndex:indexPath.section];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
  businesses = [sortedArray filteredArrayUsingPredicate:predicate];

  if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    cell.textLabel.text =
    [self.searchResults objectAtIndex:indexPath.row];
  }
  else{
        NSString *cellValue = [businesses objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
  }
  return cell;

  }

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSString *selected = nil;

 if (tableView == self.tableView)
 {
    selected = [businesses objectAtIndex:indexPath.row];
 }
 else if (tableView == searchDis.searchResultsTableView)
 {
    selected = [filteredData objectAtIndex:indexPath.row];
 }

 [def setObject:selected forKey:@"NameChoiceDetail"];

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
 {
    [self performSegueWithIdentifier:@"NameDetailPush" sender:self];

 }
 }

// お粗末なコードで申し訳ありません。私は、Objective-C を使って 4 か月、プログラミングを使って約 8 か月しか働いていません。提案/最適化は、正式に記録されます。

4

1 に答える 1

0

テーブル ビューではセクションが使用されていますが、の実装でtableView:didSelectRowAtIndexPath:はインデックス パスのセクションが評価されません。そのため、コードに何かが欠けています。

さらに、businesses変数 (おそらくインスタンス変数) の使い方が非常に奇妙だと思います。値が割り当てられていますが、そこで使用さtableView:cellForRowAtIndexPath:れていませんtableView:didSelectRowAtIndexPath:。したがって、後者の場合の結果は、最後に表示されたテーブル セルに依存し、その結果、スクロールするユーザー インタラクションに依存します。これが、結果がかなりランダムに見える理由かもしれません。

于 2012-07-23T14:57:15.530 に答える