我是iOS开发的新手。我需要在不使用Storyboard的情况下使用CollectionView,并且必须在选项卡中加载此视图。这可能。
谢谢
答案 0 :(得分:0)
雅。你可以用编程方式创建它
-(void)loadView
{
[super loadView];
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
[self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[self.collectionView setBackgroundColor:[UIColor clearColor]];
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"Cell"];
[self.view addSubView:self.collectionView];
}
答案 1 :(得分:0)
您必须创建一个UIViewController子类,它实现UICollectionViewDataSource和UICollectionViewDelegate,如下所示:
@interface MyViewController:UIViewController<UICollectionViewDelegate, UICollectionViewDataSource>
@end
然后在 .m 文件中实施所需的UICollectionViewDataSource和UICollectionViewDelegate方法
和覆盖 - [UIViewController viewDidLoad]如下:
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionView *collectionView = [UICollectionView alloc] initWithFrame:self.view.bounds];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
}