【问题标题】:CollectionView window stays in blackCollectionView 窗口保持黑色
【发布时间】:2013-01-17 17:38:36
【问题描述】:

我正在编写 iOS 应用程序。我有 CollectionView 窗口,并在其中通过拖放添加了一个 customCell。当我运行应用程序时,CollectionView 窗口的一部分是黑色的。 集合可重用视图标识符设置为“ItemCell”。自定义视图单元设置为“CustomViewCell”类。 CollectionView 的 dataSource 和 delegate 已设置为 FirstViewController。 那是代码:

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

.m 文件:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}
-(void)viewDidAppear:(BOOL)animated
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 10;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CustomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemCell" forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[CustomViewCell alloc]init];
    }


    cell.label.text = @"KitKat";
    //cell.lblMain.textColor = [UIColor whiteColor];
    // cell.backgroundColor = [UIColor blackColor];

    return cell;
}


- (IBAction)slideRed:(id)sender {
}

- (IBAction)slideGreen:(id)sender {
}

- (IBAction)slideBlue:(id)sender {
}

- (IBAction)btnAdd:(id)sender {
}
@end

自定义视图单元:

#import "CustomViewCell.h"

@implementation CustomViewCell
@synthesize label;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



@end

如何强制collectionView 窗口显示带有标签的customCell。然而它只是黑窗。 最好的问候

【问题讨论】:

标签: ios objective-c uiviewcontroller uicollectionview custom-cell


【解决方案1】:

您可能需要一个额外的步骤(我认为是类型转换),我发现这有助于将整个过程联系在一起。这是我要比较的代码:

- (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 registerClass:[CustomViewCell class] forCellWithReuseIdentifier:@"collectCell"];

【讨论】:

    【解决方案2】:

    无法使用默认透明度。但是一个解决方法解决了这个问题。 1. 选择collectionView。 2. 通过单击“背景”属性添加颜色。 3.选择一种颜色(在我的例子中是“白色”) 4.将颜色“不透明度”设置为“0%” vola.. 你会得到一个透明的背景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多