2015-04-27 28 views
0

我在故事板中創建了一個UICollectionView,並將它的類型更改爲自定義,並選擇了我的UICollectionViewLayout子類作爲它的類。UICollectionViewLayout子類

在UICollectionViewLayout子類中有以下代碼。但是當我運行應用程序時,我在collectionview中沒有看到任何東西。請問我錯過了什麼?

@implementation CollectionViewLayout 

- (id)initWithCoder:(NSCoder*)aDecoder 
{ 
    if(self = [super initWithCoder:aDecoder]) { 
     // Do something 
     NSLog(@"init with coder"); 

     self.collectionView.backgroundColor = [UIColor redColor]; 

     self = [super init]; 

     self.imageArray = [NSMutableArray array]; 
     for(int i = 0; i <32; i++) 
     { 
      NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", i]; 
      UIImage *image = [UIImage imageNamed:imageToLoad]; 
      [self.imageArray addObject:image]; 
     } 

     longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)]; 
     longPress.delegate = self; 
     [self.collectionView addGestureRecognizer:longPress]; 

     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; 
     tapGesture.numberOfTapsRequired = 2; 
     [self.collectionView addGestureRecognizer:tapGesture]; 

     self.collectionView.delegate = self; 
     self.collectionView.dataSource = self; 
    } 
    return self; 
} 

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; 
{ 
    return [self.imageArray count]; 
} 

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

    NSLog(@"cell for item"); 

    Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath]; 
    [[cell viewWithTag:999] removeFromSuperview]; 
    self.isDeleteActive = NO; 
    cell.image.image = [self.imageArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

回答

0
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; 
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 

這些方法是從不應該在自定義佈局類實現「UICollectionViewDataSource」。

相關問題