0

カスタム セルをテーブル ビューに読み込んでいますが、セルが正しく再利用されていないことに気付きました。NSFetchedResultsController を使用して Core Data から結果を取得しています。

ペン先からセルをロードしています。セル識別子は、インターフェイス ビルダーで設定されます。テーブルをスクロールするたびに新しいセルを作成していないため、セルが再利用されているように見えます。ただし、データがセルに正しく表示されていません。

// BeerCell.h
@interface BeerCell : UITableViewCell

@property (nonatomic, strong) IBOutlet UIImageView *beerImage;
@property (nonatomic, strong) IBOutlet UILabel *displayBeerName;
@property (nonatomic, strong) IBOutlet UILabel *displayBeerType;

@end

// BeerCell.m
@implementation BeerCell

@synthesize beerImage;
@synthesize displayBeerName;
@synthesize displayBeerType;

@end

 // Code where i'm setting up the cells for the tableView

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

    BeerCell *cell = (BeerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BeerCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){

            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (BeerCell *) currentObject;
                break;
            }
        }

        [self configureCell:cell atIndexPath:indexPath];

    }        

    return cell;
}

- (void)configureCell:(BeerCell *)cell 
          atIndexPath:(NSIndexPath *)indexPath 
{
    Beer *beer = (Beer *) [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.displayBeerName.text = beer.name;
}
4

1 に答える 1

1

if ブロックの外で configureCell 関数呼び出しを行います。

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

    BeerCell *cell = (BeerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BeerCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){

            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (BeerCell *) currentObject;
                break;
            }
        }
    }        
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}
于 2012-07-16T13:42:47.820 に答える