简体   繁体   中英

How can I make a collectionView without using storyboard?

I'm creating a swipe to unlock like apple using collectionView and I'm doing it in collectionView class.

Can anyone tell me how I can override collectionView class so that I can make only 2 cell of width same as superview? So when I scroll in one direction I can make other cell visible. I'm doing it like this.

#import "CustomCollectionView.h"

@implementation CustomCollectionView


- (void)drawRect:(CGRect)rect {
// Drawing code
 }


- (id)initWithFrame:(CGRect)frame {
     if (self == [super init]) {
    if (self) {
        [self customInit];
    }
}
return self;
}

 - (id)initWithCoder:(NSCoder *)aDecoder {
   self = [super initWithCoder:aDecoder];
     if (self) {
     [self customInit];
 }
 return self;
 }



 - (void)customInit {

     UICollectionViewLayout *layout = [[UICollectionViewLayout         alloc]init];
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 50) collectionViewLayout:layout];



collectionView.delegate = self;
collectionView.dataSource = self;
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseIdentifier"];
[self setBackgroundColor:[UIColor grayColor]];

}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}



 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
return cell;
}



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

But it is showing me nothing.

检查是否已连接数据源并使用文件检查器委托。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM