0

UILabel2 行の CustomViewCell に問題があります。

最初にCollectionView. 1 行のみの場合UILabelは、タイトルが一番上に表示されます。ただし、リストの最後までスクロールして最初に戻ると、すべてUILabelsが2行になり、タイトルが途中で分割されることがあります

前: 素晴らしいタイトル

後:素晴らしい

eタイトル

と関係があると確信していますがreusable Cells、何が間違っているのかわかりません...

セルの textLabel を次のように設定します。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    MyItem* a = [self.fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
    CustomSelectionView *bookCell = [collectionView dequeueReusableCellWithReuseIdentifier:CustomSelectionCell_IDENTIFIER forIndexPath:indexPath];

    //CELL CONTAINS an UIImage (Cover) and two UILabels (Author, Title)

    //get image from storage
    NSString* isbn = a.isbn;
    NSString* filePath = [_mamPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", isbn]];

    FDIBookCollectionViewCell* __weak weakCell = bookCell;    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        NSData* data = [NSData dataWithContentsOfFile:filePath];
        UIImage* image = [UIImage imageWithData:data scale:[[UIScreen mainScreen] scale]];

        if (!image) {
            //... handle
        }

        if (image && weakCell) {

            dispatch_async(dispatch_get_main_queue(), ^{
                weakCell.imageView.image = image;
                [weakCell.imageView setNeedsDisplay];
            });

        }

    });


    //set author and title
    bookCell.titelLabel.text = a.titel;

    bookCell.titelLabel.numberOfLines = 2;  //title label can have two lines
    [bookCell.titelLabel sizeToFit];

    return bookCell;
} 

セル用 InterfaceBuilder:

ここに画像の説明を入力

BookCell.m

    @implementation FDIBookCollectionViewCell {}

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {

        }
        return self;
    }

    //...
    - (void)prepareForReuse {
        [super prepareForReuse];

        self.imageView.image = nil; 
    }

    @end

誰でもアイデアはありますか?

4

1 に答える 1

1

あなたは近いです、私は問題がスクロール中にセルが再利用のために準備されているときに関係していると思います。すでに画像を nil に設定していますが、prepareForReuseこれは適切です。ラベルを nil に設定するだけで、メソッドは次のようになります。

-(void)prepareForReuse
{
    [super prepareForReuse];

    self.imageView.image = nil;
    self.titelLabel = nil;   
}

うまくいくはずです!

編集:

カスタム セル ヘッダーで、setContent メソッドとラベル変数を定義します。

-(void)setContentWithDictionary:(NSDictionary *)content;

@property (nonatomic, strong) UILabel *titelLabel;

次に、実装ファイルで次のようにします。

-(void)setContentWithDictionary:(NSDictionary *)content
{
    //Initialise your labels here
    titelLabel = [[UILabel alloc] init];
    //Customise label and set text
    [titelLabel setText:[content valueForKey:@"CellTitle:]];
}

それで:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Most of your current code
    //Not tested this line, probably won't work as is but you get the idea!
    NSDictionary *cellContent = [NSDictionary dictionaryWithValuesAndKeys:[a.titel], @"CellTitle"];
    CustomSelectionView *bookCell = [collectionView dequeueReusableCellWithReuseIdentifier:CustomSelectionCell_IDENTIFIER forIndexPath:indexPath];
    [bookCell setContent:cellContent];
    return bookCell;
}

セルの他の要素ごとに、辞書に新しい要素を追加するだけです。このようにして、セルを描画する必要があるたびに、正しいコンテンツが取得され、ラベルが正しい方法で設定されます。

うまくいけば、これはすべて意味があり、助けになります!

マット

于 2013-09-17T15:25:10.017 に答える