Objective c UICollectionCell中具有两行奇怪行为的UILabel
我对CustomViewCell有一些问题,它有一个有两行的Objective c UICollectionCell中具有两行奇怪行为的UILabel,objective-c,uilabel,uicollectionviewcell,Objective C,Uilabel,Uicollectionviewcell,我对CustomViewCell有一些问题,它有一个有两行的UILabel 如果我第一次显示我的CollectionView,一切看起来都像预期的那样。如果UILabel只有一行,则标题位于顶部。但是,如果我开始滚动到列表的末尾,而不是回到顶部,所有 uILabels都有两行,有时标题在中间被分割为 前:令人敬畏的头衔 之后:Awesom 艾蒂特尔 我确信这与可重用单元有关,但我不知道我做错了什么 我为单元格设置textLabel,如下所示: - (UICollectionViewCell *)
UILabel
如果我第一次显示我的CollectionView
,一切看起来都像预期的那样。如果UILabel
只有一行,则标题位于顶部。但是,如果我开始滚动到列表的末尾,而不是回到顶部,所有<代码> uILabels<代码>都有两行,有时标题在中间被分割为
前:令人敬畏的头衔
之后:Awesom
艾蒂特尔
我确信这与可重用单元有关,但我不知道我做错了什么
我为单元格设置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;
}
单元格的接口生成器:
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
有人有什么想法吗 很接近了,我认为问题在于单元格何时准备在滚动过程中重用。您已经在
prepareforeuse
中将图像设置为nil,这很好,您只需在此处将标签设置为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:]];
}
然后:
对于单元格中的每个其他元素,只需在字典中添加一个新元素。这样,每次需要绘制单元格时,它都会获得正确的内容,并且标签将以正确的方式设置
希望这一切都有意义和帮助
Matt设置titelLabel.text=nil;不起作用,但将整个标签设置为零就成功了!谢谢你把我推向正确的方向。我编辑了你的答案,并接受了如果我是一个男孩,我可以帮助!我想这取决于对象初始化的方式/时间。如果在interface builder中引用标签或其他对象,则只需将文本设置为nil。我猜你是在用代码初始化所有东西?(这可能是更好的方法,但这是一个完全不同的主题!)在这种情况下,是的,您希望将整个标签设置为nil,以便重新初始化!是 啊我才意识到现在有时候标签已经不对了。。有什么建议吗?不正确吗?单元格是如何定义的?您在哪里初始化标签?您能解释一下单元格是如何不正确的吗?问题可能是,在PrepareForuse中,您将单元格设置为nil,然后我假定它在之后没有完全重新初始化。我不知道你的细胞将有什么样的生命周期,因为我不知道你是从故事板或IB文件中引用的,还是所有的东西都是通过编程创建的。您只需要确保在向单元格提供数据时,您传递了数据并重新初始化了所有内容。我将为我的答案添加一个编辑,这将有助于您完成此操作。
- (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;
}