Ios UICollectionView的问题:不显示其所有单元格的标签
我以编程方式创建了UICollectionView。我使用自定义UICollectionViewCell子类。在cell类中,我使用自己的类创建了一个标签(设置其外观更容易、更快速)。 我遇到的问题是:对于几个单元格,collectionView没有布局标签内容。我知道数据在这里(在控制台中打印),也就是说,单元格的text属性确实包含我想要显示的字符串数据,但由于某些原因,collectionView不显示标签内容。 我尝试了一个简单的测试(在标签内打印“toto”),我在这里和那里得到了一些toto,但不是在所有的单元格中。正如您所看到的,我在同一个ViewController中有2个UICollectionView,这就是为什么我在DataSource实现中测试它是一个还是另一个 如果您需要更多代码,请告诉我 代码如下:Ios UICollectionView的问题:不显示其所有单元格的标签,ios,objective-c,uilabel,uicollectionview,uicollectionviewcell,Ios,Objective C,Uilabel,Uicollectionview,Uicollectionviewcell,我以编程方式创建了UICollectionView。我使用自定义UICollectionViewCell子类。在cell类中,我使用自己的类创建了一个标签(设置其外观更容易、更快速)。 我遇到的问题是:对于几个单元格,collectionView没有布局标签内容。我知道数据在这里(在控制台中打印),也就是说,单元格的text属性确实包含我想要显示的字符串数据,但由于某些原因,collectionView不显示标签内容。 我尝试了一个简单的测试(在标签内打印“toto”),我在这里和那里得到了一些
-(void)createBottomCollectionView {
// Layout
UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
collectionViewLayout.minimumLineSpacing = 0.0;
// UICollectionView
self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 354+20, 320-2*20, 35) collectionViewLayout:collectionViewLayout];
self.bottomCollectionView.showsHorizontalScrollIndicator = NO;
self.bottomCollectionView.bounces = YES;
self.bottomCollectionView.alwaysBounceHorizontal = YES;
self.bottomCollectionView.alwaysBounceVertical = NO;
self.bottomCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.bottomCollectionView.dataSource = self;
self.bottomCollectionView.delegate = self;
[self.bottomCollectionView registerClass:[SetFormatCollectionViewCell class] forCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier];
// Background
self.bottomCollectionView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.bottomCollectionView];
[self.bottomCollectionView reloadData];
}
CollectionView数据源(带“toto”值的哑测试)在真实应用程序中,我使用CoreData提取数据
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.bottomCollectionView) {
SetFormatCollectionViewCell *cell = (SetFormatCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier forIndexPath:indexPath];
cell.text = @"toto";
return cell;
}
if (collectionView == self.collectionView) {
TrackingSetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
return nil;
}
自定义单元类:
@interface SetFormatCollectionViewCell : UICollectionViewCell
@property (strong,nonatomic) NSString *text;
@end
@implementation SetFormatCollectionViewCell
{
FormatLabel *aFormatLabel;
}
-(id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
// Initialization code
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.frame textColor:[UIColor blackColor] font:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];
[self.contentView addSubview:aFormatLabel];
}
return self;
}
-(void)prepareForReuse {
[super prepareForReuse];
self.text = @"";
}
-(void)setText:(NSString *)text {
_text = [text copy];
aFormatLabel.text = self.text;
}
FormatLabel类(我认为不重要)
谢谢你的帮助
编辑:对于那些可能有相同问题的人,我发布了问题的3张照片(你可以在下面找到答案)。第二张照片包含彩色单元格,请参见问题所在。第三张照片是我在接受jmkk的回答后拍的
谢谢所有其他的答案
您的问题在于单元格中FormatLabel视图的定位。 您正在使用单元格的框架作为标签的框架,而您需要的是单元格的边界 单元的帧相对于其超级视图,因此将相同的位置应用于单元的子视图将使其相对于单元本身偏移 修复代码以执行此操作:
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.bounds textColor:[UIColor blackColor] font:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];
你能截图吗?@batistomorrow你确定你的集合视图得到的是SetFormatCollectionViewCell的单元格吗。调试它once@thewormsterror我很想,但我需要10分的声誉。。。在图片中,您将看到一个水平集合视图,所有单元格都在那里(如果我设置它们的背景颜色,您将看到所有单元格),但表示只有第一个和第三个单元格会显示标签,而其他单元格将保持为空。@Jeev Yes,我检查了,CV采用了好的单元格。“我觉得prepareForReuse或其他东西有问题,但我不明白为什么它会正确地布局某些单元格,并将其他单元格搞得一团糟。”batistomorrow我投了赞成票,现在你可以发布SS了
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.bounds textColor:[UIColor blackColor] font:[UIFont fontWithName:@"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];