4

我在寫iOS應用程序。我有CollectionView窗口,裏面有一個通過拖放添加的customCell。當我運行應用程序時,CollectionView窗口的一部分是黑色的。 Collection可重用視圖標識符設置爲'ItemCell'。自定義視圖單元格設置爲「CustomViewCell」類。 CollectionView的dataSource和delegate已被設置爲FirstViewController。 這是代碼:CollectionView窗口保持黑色

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 

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與標籤。但它只是黑色的窗口。 此致敬禮

+1

您是否已將集合視圖的委託/數據源設置爲此視圖控制器? –

+0

是的,我做到了。 – uml

+1

初始化您的標籤,請參閱:http://stackoverflow.com/questions/14287353/error-setting-text-in-collection-view-cell/14289862#14289862 –

回答

1

您可能需要額外的步驟(我認爲類型轉換),我發現幫助將整個過程鏈接在一起。這裏,是我的代碼進行比較:

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

    return customCell; 
} 

WWW的評論是正確的,你需要初始化你的UI元素(id)initWithFrame:(CGRect)frame,並將它們添加到您的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%」 揮發..你會得到一個透明的背景。