11

したがって、セクションと行を持つ tableView があり、カスタム セル クラスを使用しています。カスタム セルには、画像ビューといくつかのラベルがあります。テーブルビューは正常に機能し、検索は機能しますが、検索ではカスタムセルクラスにあるラベルが表示されず、正しい画像を持つ imageView のみが表示されます。特に画像はまだ表示されていますが、ラベルは表示されていないため、これがなぜなのか非常に混乱しています。ここにいくつかのコードがあります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


//TODO: problem with search view controller not displaying labels for the cell, needs fixing
JSBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(cell == nil) {
    cell = [[JSBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

JSBook *book = nil;
//uses the appropriate array to pull the data from if a search has been performed
if(tableView == self.searchDisplayController.searchResultsTableView) {
    book = self.filteredTableData[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}
else {
    book = self.books[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}

FFMetaData *data = [self.ff metaDataForObj:book];
cell.titleLabel.text = book.title;
cell.priceLabel.text = [NSString stringWithFormat:@"$%@", book.price];
cell.authorLabel.text = book.author;
cell.descriptionLabel.text = book.description;

cell.dateLabel.text = [self.formatter stringFromDate:data.createdAt];
if(book.thumbnail == nil) {
    cell.imageView.image = [UIImage imageNamed:@"messages.png"];
    [self setCellImage:cell withBook:book atIndex:indexPath withTableView:tableView];
}
else {
    cell.imageView.image = [UIImage imageWithData:book.thumbnail];
}

return cell;

}

この問題が発生する前は、tableView にセクションが 1 つしかなく、すべてが完全に機能していました。複数のセクションと行があるため、説明したように検索が壊れています。何か案は?また、[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];以前は持っていましたが[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];、今はそれを使用すると、検索しようとすると奇妙な例外が発生します。

NSInternalInconsistencyException'、理由: '無効なインデックス パスでの rect の要求 (2 つのインデックス [1, 1])'

だから私も混乱しています。助けてくれてありがとう!

4

6 に答える 6

30
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

テーブル ビュー セルが特定のテーブル ビューに登録されているため、機能しませんでした。これは、検索結果コントローラー テーブル ビューでは機能しません。あなたはこれを自分で見つけて、次のように切り替えました:

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

これは正しいことです。

また、ストーリーボードでカスタム セルを設計しても、検索結果コントローラーでは実際には機能しません。これは、検索テーブル ビューのセルを設計できず、メイン テーブル ビューに対してのみ設計できるためです。

はい、ここで行ったように、そのクラスを検索テーブル ビューに登録できます。

[self.searchDisplayController.searchResultsTableView registerClass:[JSBookCell class] forCellReuseIdentifier:CellIdentifier];

ただし、ストーリーボードのカスタムセルに設計したものはありません。プログラムですべてを作成する必要があります。

于 2013-04-09T06:42:15.020 に答える
9

ストーリーボードプロトタイプで設計された同じカスタムセルを使用して、検索バーと検索表示を備えた UITableView の私の要約:

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

    CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        /* searchResultsTableView code here */
    } else {
        /* Base tableView table code here */
    }

    /* more cell code here */

    return cell;
}


次に、searchResultsTableView に次の行を追加して、カスタム セルの高さに合わせます。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.searchDisplayController.searchResultsTableView setRowHeight:self.tableView.rowHeight];

     /* more of your viewDidLoad code */
}
于 2013-12-20T04:41:49.900 に答える
6

UIViewController で UITableView を使用していて、ストーリーボードで作成したセル識別子を searchDisplayController に再利用したい場合は、次のようにします。

StoryBoard > UIViewController > Reference Outlets > tableView を UIViewController の .h ファイルにリンクし、tableView のような名前を付けると、次のようになります。

@property (strong, nonatomic) IBOutlet UITableView *tableView;

したがって、次のようにするのではなく:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}

これを行う

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}
于 2013-10-21T10:46:38.180 に答える
0

これで試してみてください、それは私にとってはうまくいきます

JSBookCell * cell = [yourtableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
于 2015-03-11T11:38:02.137 に答える