以编程方式创建UICollectionView我正在寻找一个指南或教程,它将向我展示如何仅使用代码设置一个简单的UICollectionView。我正在浏览苹果网站上的文档,我也在使用参考手册。但我真的可以从一个简单的指南中受益,它可以告诉我如何设置UICollectionView而不必使用Storyboard或XIB / NIB文件 - 但不幸的是,当我搜索时,我能找到的只是具有故事板的教程。
3 回答
萧十郎
TA贡献1815条经验 获得超13个赞
头文件: -
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{ UICollectionView *_collectionView;}
实施档案: -
- (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贡献1765条经验 获得超5个赞
对于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贡献1811条经验 获得超5个赞
对于Swift 2.0
而不是实现绘制以下方法所需的方法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
根据需要实现方法:
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 关注
- 588 浏览
添加回答
举报
0/150
提交
取消