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:
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?