4

I am writing iOS app. I have CollectionView window and inside it one customCell added by dragging and dropping in. When I run the app that part of CollectionView window is black. Collection reusable view identifier is set to 'ItemCell'. Custom View Cell set to class 'CustomViewCell'. CollectionView's dataSource and delegate have been set to FirstViewController. That is the code:

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 file:

#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

CustomViewCell:

#import "CustomViewCell.h"

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



@end

How to force that collectionView window show the customCell with the label. Yet it is simply black window. Best regards

Luke
  • 11,426
  • 43
  • 60
  • 69
uml
  • 1,171
  • 4
  • 16
  • 28

2 Answers2

2

Couldn't get the default transparency to work. But a workaround solved this. 1. Select the collectionView. 2. Add a color by clicking on the "background" property. 3. Select a color (in my case "white") 4. Set the color "opacity" to "0%" vola.. you get a transparent background.

varunarl
  • 291
  • 3
  • 4
1

you may need an additional step (type casting I think) that I found helped link the whole process together. Here, is my code to compare:

- (CustomViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"collectCell";
    CustomViewCell *customCell = (CustomViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    return customCell;
}

www's comment is correct, you need to initialize your UI elements in (id)initWithFrame:(CGRect)frame and add them into your self.contentView addSubview:viewElement in that same method. For example:

- (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;
} 

You also may want to try this:

[collectionView registerClass:[CustomViewCell class] forCellWithReuseIdentifier:@"collectCell"];

Michael Lorenzo
  • 628
  • 10
  • 20