簡體   English   中英

自定義單元重用問題

[英]Custom Cell Reuse Issue

我正在將自定義單元格加載到表視圖中,請注意我的單元格未正確重復使用。 我正在使用NSFetchedResultsController從核心數據中提取結果。

我正在從筆尖裝載細胞。 單元標識符在接口構建器中設置。 單元格似乎被重新使用,因為每次滾動表格時我都不會創建新的單元格。 但是,數據未正確顯示在單元格上。

// 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;
}

在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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM