2014-02-24 25 views
0

我有一個UIViewController其上有一個UICollectionView,但Xcode看起來不像我在互聯網或YouTube上找到的每個教程 - 當我拖動一個UICollectionViewCell以放置在UICollectionView ,它不會讓我放置它。如何將UICollectionViewCell鏈接到UICollectionView

現在我很困惑我如何將我的細胞連接到UICollectionView。 這是viewController.h文件:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
return 1; 
} 
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
return [self.imagesArray count]; 
} 

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

ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath]; 



NSString *myImageString = [self.imagesArray objectAtIndex:indexPath.row]; 
cell.imageView.image = [UIImage imageNamed:myImageString]; 


return cell; 
} 

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
return CGSizeMake(100.0, 100.0); 
} 

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ 
return UIEdgeInsetsMake(5, 5, 5, 5); 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.collectionView.delegate = self; 
self.collectionView.dataSource = self; 


//[self.collectionView registerClass:[ImageViewCell class] forCellWithReuseIdentifier:@"imageCell"]; 

self.imagesArray = @[@"shirt1.PNG", @"pants.png", @"pants2.png"]; 
} 
我沒有用故事板界面,但個別廈門國際銀行的

。當我運行這一切時出現的是空白的黑屏。我錯過了什麼?

回答

1

-registerNib:forCellWithReuseIdentifier:

如果你有一個NIB定義你的,那麼你註冊該筆尖與集合視圖。收集視圖在調用-dequeueReusableCellWithReuseIdentifier:forIndexPath:時知道如何加載。

- (void)viewDidLoad 
{ 
    // … 

    UINib nib = [UINib nibWithNibName:@"<the name of your xib>" bundle:nil]; 
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"imageCell"]; 

    // … 
} 
0

因爲你沒有一個新的這種方法的UICollectionViewCell對象:

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

在你的方法中的「單元」必須設置爲nil

相關問題