13

テーブルビューには、UITableViewCell クラスから初期化するカスタム セルがあります。レコードの最初の文字のセクションがあり、動的に作成される indexPath があります。

テーブルビューに検索表示コントローラーを追加したかったのです。だから私は、データをフィルタリングするためのすべてのメソッドを作成しました。検索結果の画面に配列カウントを出力しているので、関数がうまく機能していると確信しています。

私の問題は、ビューが初めて読み込まれるときに、データが画面に表示されることです。しかし、検索入力を押して文字を入力すると、'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'エラーが発生します。ブレークポイントを使用した後、検索後にカスタム セルが nil であることがわかりました。データは存在しますが、セルは初期化されていません。

カスタムセルの初期化に使用するコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ObjectCell";

    SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *myObject = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    cell.label1.text = [myObject objectForKey:@"myValue"];
    return cell;
}

IB にコントロールを配置するときに間違いを犯したと思います。そこで、オブジェクトのスクリーンショットを追加しました:

ここに画像の説明を入力

テーブルビューの接続インスペクター

テーブルビューの接続インスペクター

私の検索表示コントローラーの接続インスペクター

ここに画像の説明を入力

編集: 問題は実際に解決されました。検索ディスプレイ コントローラーの代わりに UISearchBar を使用しましたが、この問題は未解決のままだと思います。ですから、私はそれを機能させるためにあらゆる方法を試してみたいと思っています。

4

4 に答える 4

54

As of here search display controller question, you need to access the self.tableView instead of tableView:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CellId"];

    // do your thing

    return cell;
}
于 2012-08-06T18:24:41.043 に答える
2

iOS 5 と StoryBoards を使用している場合は、initWithIdentifierの代わりに次のメソッドを使用することをお勧めします。

initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier

例:

  NSString *cellIdentifier = @"ListItemCell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }
于 2012-08-16T06:23:21.167 に答える
0

これがストアボーディングでどのように機能するかはわかりません。

ただし、通常[tableView dequeueReusableCellWithIdentifier:CellIdentifier]は、セルが返されるかどうかを確認します。セルが以前にロードされていない場合、または再利用するセルがない場合は、新しいセルを作成する必要があるためです。

SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
   cell = [[SpeakerCell alloc] initWithIdentifier: CellIdentifier];
}

また、Objective-C でローカル変数を宣言するときに、最初の文字を大文字にしない傾向があります。

于 2012-04-17T10:21:10.530 に答える