我在寫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與標籤。但它只是黑色的窗口。 此致敬禮
您是否已將集合視圖的委託/數據源設置爲此視圖控制器? –
是的,我做到了。 – uml
初始化您的標籤,請參閱:http://stackoverflow.com/questions/14287353/error-setting-text-in-collection-view-cell/14289862#14289862 –