【问题标题】:Instead of UICollectionView a black screen is displayed显示黑屏而不是 UICollectionView
【发布时间】:2015-03-31 10:37:50
【问题描述】:

我正在尝试重新实现UICollectionView 看到的here 的不定式滚动。我缺少的东西:

ViewController.h:

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

@end

DataCell.h:

@interface DataCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *label;
@end

DataCell.m:

#import "DataCell.h"

@implementation DataCell

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        self.label = [[UILabel alloc] initWithFrame:self.bounds];
        self.autoresizesSubviews = YES;
        self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                       UIViewAutoresizingFlexibleHeight);
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.adjustsFontSizeToFitWidth = YES;

        [self addSubview:self.label];
    }

    return self;
}

@end

CustomCollectionView.h:

@interface CustomCollectionView : UICollectionView

@end

对于整个项目,我使用了一个故事板和一个普通的UIViewController。在这个视图控制器上,我在 Interface Builder 中添加了一个 UICollectionView。我将集合视图的出口与我的视图控制器连接起来,并将数据源和委托方法再次设置到我的视图控制器。我还在Interface Builder中设置了UICollectionViewCell的自定义类和重用标识符。

所以一切都应该工作,但我只得到一个黑屏。我错过了什么?你可以下载整个项目here。

【问题讨论】:

    标签: ios objective-c interface-builder uicollectionview infinite-scroll


    【解决方案1】:

    您正确配置了 CollectionView,只是您忘记了标签的颜色 :)

        [self.label setTextColor:[UIColor whiteColor]];
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      您需要在storyboard中手动设置collection view的背景颜色。

      默认情况下它是黑色的(虽然没有在故事板编辑器中显示)

      【讨论】:

      • 如果想要背景清晰,必须使用collectionView.backgroundColor = [UIColor clearColor];和collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
      【解决方案3】:

      我有同样的问题。黑屏似乎表明没有可用的数据可用于显示集合视图。尝试更改集合视图的背景颜色,如果要显示更改的颜色,则您的集合视图正在工作。然后使用标记将一些图像视图添加到集合视图(例如,使用图像视图的标记值给出值 100)并使用 cellforItemAtIndexPath 将图像设置为图像视图。 (您可以使用自定义单元格来执行此操作。但现在,为了使集合视图正常工作,图像视图的标记分配更适合)

      UIImageView * ImageView = (UIImageView *)[cell viewWithTag:100];
      ImageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
      

      【讨论】:

        【解决方案4】:
        [self.collectionView registerNib:[UINib nibWithNibName:@"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ProductCollectionViewCell"];
        
        self.collectionView.backgroundColor = [UIColor clearColor];
        

        【讨论】:

          【解决方案5】:

          我碰巧collectionView和collection view cell都有透明背景

          【讨论】:

            【解决方案6】:

            在 Swift 中,

            self.label.textColor = UIColor.whiteColor()
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-10-21
              • 1970-01-01
              • 2018-06-18
              • 2014-12-07
              • 1970-01-01
              • 1970-01-01
              • 2023-02-07
              • 1970-01-01
              相关资源
              最近更新 更多