お世話になります!
初歩的で恐縮ですが、1つご質問させてください。
UICollectionViewを別ファイルで定義して、ViewContorollerで呼び出すコードを書きたいのですが、方法がわかりません。
以下に現在のコードを掲載いたしますので、ご指摘いただけましたらと思ってます!
###ソースコード
swift
1 2import UIKit 3 4class ViewController: UIViewController { 5 6 var CollectionView: UICollectionView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 12 let collection = CollectionView 13 collection 14 15 16 17 18 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 // Dispose of any resources that can be recreated. 24 } 25 26 27}
###ソースコード
swift
1import UIKit 2 3class CollectionView: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource { 4 5 func collection() -> UICollectionView { 6 var CollectionView: UICollectionView! 7 8 let layout = UICollectionViewFlowLayout() 9 10 layout.itemSize = CGSizeMake(50, 50) 11 12 layout.headerReferenceSize = CGSizeMake(100, 30) 13 14 CollectionView = UICollectionView(frame: self.view.frame,collectionViewLayout: layout) 15 16 CollectionView.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 17 18 CollectionView.delegate = self 19 20 CollectionView.dataSource = self 21 22 self.view.addSubview(CollectionView) 23 24 return CollectionView 25 } 26 27 28 /* 29 Cellが選択された際に呼び出される 30 */ 31 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 32 33 print("Num: \(indexPath.row)") 34 35 } 36 37 /* 38 Cellの総数を返す 39 */ 40 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 41 return 100 42 } 43 44 /* 45 Cellに値を設定する 46 */ 47 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 48 49 let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CustomUICollectionViewCell 50 cell.textLabel?.text = indexPath.row.description 51 52 return cell 53 } 54 55} 56 57
以上、お願いします!
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。