4

tableview検索バーで検索すると、このエラーが表示され、セルがないことを示し、以下のエラーが表示されます。このメソッドでセルを作成するにはどうすればよいですprototypeCellForRowAtIndexPath

コード :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
 static NSString *CellIdentifier = @"HoCell";
Ho *cell;
Ho *item;

if (tableView == self.searchDisplayController.searchResultsTableView) {
    if (cell == nil)
    {
        cell = [[Ho alloc]  initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"HoCell"];
    }
    item = [searchResultsController objectAtIndexPath:indexPath];
}
else{
    cell = (Ho*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    item = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.ho.text = item.name;

cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"se.png"]];

return cell;
}

エラー :

*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:],  /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:5471
 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
4

3 に答える 3

4

ここでは2つの可能性があります:

1)tableView:numberOfRowsInSection:から配列カウントよりも大きい数を返しています。しないでください。

2)1つ以上のcell#アウトレットがペン先に接続されていないか、UITableViewCell(またはサブクラス)に接続されていません。それらを適切に接続します。

このRayWenderlichのリンクを確認してください: テーブルビューに検索を追加する方法

このSOの質問を確認してください:

1) UITableView dataSourceは、tableView:cellForRowAtIndexPathからセルを返す必要があります:例外

2) ios5UISearchDisplayControllerのクラッシュ

もう1つの美しいリンク:カスタムプロトタイプテーブルセルとストーリーボードこの部分をご覧ください:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView
            dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier];
  if (cell == nil)
  {
    [self.countryCellNib instantiateWithOwner:self options:nil];
    cell = self.countryCell;
    self.countryCell = nil;
  }
  // Code omitted to configure the cell...
  return cell;
}
于 2013-01-07T08:12:37.690 に答える
0

あなたのコードはバグがあるようです。cell == nil をチェックしますが、最初は nil に設定されていません。また、検索モードに基づいてそのようにセルを割り当てるのも少し奇妙に見えます。

とにかく、私はそれを別の方法で行います。私のやり方はほとんど標準的です:)検索結果を使用して、各ケース(検索モードと通常モード)の正しいデータをセルに入力するだけです。この例では、searchResult と dataSource は文字列を含む配列です。実生活では、nsdictionary の配列のようなもっと複雑なものになると思います。

ビューコントローラーで:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)_section
{
        /* Use correct data array so that in search mode we draw cells correctly. */
        NSMutableArray *data = searching ? searchResult : dataSource;
        return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{       
        /* Use correct data array so that in search mode we draw cells correctly. */
        NSMutableArray *data = searching ? searchResult : dataSource;
        static NSString *CellIdentifier = @"CellId";

        CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
                cell = [[[CustomTableViewCell alloc] initWithIdentifier:CellIdentifier] autorelease];
        }

        /* Note this is only correct for the case that you have one section */
        NSString *text = [data objectAtIndex:[indexPath row]]

        cell.textLabel.text = text;
        /* More setup for the cell. */
        return text;
}

検索コントローラーといくつかのヘルパーのデリゲート メソッドを次に示します。

- (void) searchTableView
{
        NSString *searchText = searchBar.text;

        for (NSString *item in dataSource) {
                NSRange range = [item rangeOfString:searchText options:NSCaseInsensitiveSearch];
                if (range.length > 0) {
                        [searchResult addObject:item];
                }
        }
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
        searching = NO;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
        searching = NO;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
                      withRowAnimation:UITableViewRowAnimationAutomatic];
        [searchResult removeAllObjects];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchText
{
        [searchResult removeAllObjects];

        if ([searchText length] > 0) {
                searching = YES;
                [self searchTableView];
        } else {
                searching = NO;
        }
        return YES;
}

お役に立てれば。

于 2013-01-07T08:56:50.100 に答える