0

I have some trouble with a CustomViewCell which has a UILabel which has two lines.

Everything is looking like expected if I first show my CollectionView. If UILabel has only one line, the title is at the top. However, if I start scrolling to the end of the list and than go back to top, all UILabels have two lines and sometimes the titles are divided in the middle like

BEFORE: AwesomeTitle

AFTER: Awesom

eTitle

I'm quit sure it has something to do with reusable Cells, but I have no idea what I'm doing wrong...

I set the textLabel for the cell like:

- (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 for Cell:

enter image description here

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

Anyone any ideas?

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 回答