Ios CollectionView窗口保持为黑色
我正在编写iOS应用程序。我有一个CollectionView窗口,里面有一个通过拖放添加的customCell。当我运行应用程序时,CollectionView窗口的那部分是黑色的。 集合可重用视图标识符设置为“ItemCell”。自定义视图单元格设置为类“CustomViewCell”。CollectionView的数据源和委托已设置为FirstViewController。 这就是代码:Ios CollectionView窗口保持为黑色,ios,objective-c,uiviewcontroller,uicollectionview,custom-cell,Ios,Objective C,Uiviewcontroller,Uicollectionview,Custom Cell,我正在编写iOS应用程序。我有一个CollectionView窗口,里面有一个通过拖放添加的customCell。当我运行应用程序时,CollectionView窗口的那部分是黑色的。 集合可重用视图标识符设置为“ItemCell”。自定义视图单元格设置为类“CustomViewCell”。CollectionView的数据源和委托已设置为FirstViewController。 这就是代码: FirstViewcontroller: #import <UIKit/UIKit.h>
FirstViewcontroller:
#import <UIKit/UIKit.h>
#import "CustomViewCell.h"
@interface FirstViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *txtField;
- (IBAction)slideRed:(id)sender;
- (IBAction)slideGreen:(id)sender;
- (IBAction)slideBlue:(id)sender;
- (IBAction)btnAdd:(id)sender;
@end
CustomViewCell:
#import "CustomViewCell.h"
@implementation CustomViewCell
@synthesize label;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end
如何强制collectionView窗口显示带有标签的customCell。然而,它只是一扇黑色的窗户。
致以最诚挚的问候您可能需要一个额外的步骤(我认为类型转换),我发现这有助于将整个过程联系在一起。下面是我要比较的代码:
- (CustomViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"collectCell";
CustomViewCell *customCell = (CustomViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return customCell;
}
www的评论是正确的,您需要在(id)initWithFrame:(CGRect)frame
中初始化UI元素,并用相同的方法将它们添加到self.contentView addSubview:viewElement
中。例如:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
stringLabel = [[UILabel alloc] init];
//set the properties of your element
[self.contentView addSubview:stringLabel];
}
return self;
}
您也可以尝试以下方法:
[collectionView注册表类:[CustomViewCell类]forCellWithReuseIdentifier:@“collectCell”]代码>无法使用默认透明度。但解决办法解决了这个问题。
1.选择collectionView。
2.通过单击“背景”属性添加颜色。
3.选择一种颜色(在我的例子中为“白色”)
4.将颜色“不透明度”设置为“0%”
沃拉。。您将获得一个透明的背景。是否已将集合视图的委托/数据源设置为此视图控制器?在标签中,请参阅:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
stringLabel = [[UILabel alloc] init];
//set the properties of your element
[self.contentView addSubview:stringLabel];
}
return self;
}