以編程方式創(chuàng)建UICollectionView我正在尋找一個(gè)指南或教程,它將向我展示如何僅使用代碼設(shè)置一個(gè)簡(jiǎn)單的UICollectionView。我正在瀏覽蘋(píng)果網(wǎng)站上的文檔,我也在使用參考手冊(cè)。但我真的可以從一個(gè)簡(jiǎn)單的指南中受益,它可以告訴我如何設(shè)置UICollectionView而不必使用Storyboard或XIB / NIB文件 - 但不幸的是,當(dāng)我搜索時(shí),我能找到的只是具有故事板的教程。
3 回答

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
頭文件: -
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{ UICollectionView *_collectionView;}
實(shí)施檔案: -
- (void)viewDidLoad{ [super viewDidLoad]; self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; _collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; [_collectionView setDataSource:self]; [_collectionView setDelegate:self]; [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; [_collectionView setBackgroundColor:[UIColor redColor]]; [self.view addSubview:_collectionView]; // Do any additional setup after loading the view, typically from a nib.}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 15;}// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; cell.backgroundColor=[UIColor greenColor]; return cell;}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake(50, 50);}
輸出---

POPMUISE
TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
對(duì)于swift4用戶: -
class TwoViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate { override func viewDidLoad() { super.viewDidLoad() self.collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout) collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell") collectionView.delegate = self collectionView.dataSource = self collectionView.backgroundColor = UIColor.cyan self.view.addSubview(collectionView) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath) cell.backgroundColor = UIColor.green return cell } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: 50, height: 50) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5) }}

四季花海
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
對(duì)于Swift 2.0
而不是實(shí)現(xiàn)繪制以下方法所需的方法CollectionViewCells
:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSizeMake(50, 50); } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(5, 5, 5, 5); //top,left,bottom,right }
使用 UICollectionViewFlowLayout
func createCollectionView() { let flowLayout = UICollectionViewFlowLayout() // Now setup the flowLayout required for drawing the cells let space = 5.0 as CGFloat // Set view cell size flowLayout.itemSize = CGSizeMake(50, 50) // Set left and right margins flowLayout.minimumInteritemSpacing = space // Set top and bottom margins flowLayout.minimumLineSpacing = space // Finally create the CollectionView let collectionView = UICollectionView(frame: CGRectMake(10, 10, 300, 400), collectionViewLayout: flowLayout) // Then setup delegates, background color etc. collectionView?.dataSource = self collectionView?.delegate = self collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID") collectionView?.backgroundColor = UIColor.whiteColor() self.view.addSubview(collectionView!)}
然后UICollectionViewDataSource
根據(jù)需要實(shí)現(xiàn)方法:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20; }func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell:UICollectionViewCell=collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as UICollectionViewCell; cell.backgroundColor = UIColor.greenColor(); return cell;}func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections return 1}
- 3 回答
- 0 關(guān)注
- 636 瀏覽
添加回答
舉報(bào)
0/150
提交
取消