iPhoneアプリの作っている初心者なのですが
CollectionViewのcellを5×6のcellからnavigation barの右あるボタンを押すとcellのサイズも変わって6×6になるというプログラムを作りたいです.
この行数と列数はViewContorllerの方でメンバ変数として作ってあります
swift
1class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 2 var myCollectionView : UICollectionView! 3 var gyou:Int = 6 4 var retu:Int = 5 5 6override func viewDidLoad() { 7 super.viewDidLoad() 8 9 let width = self.view.bounds.width 10 let height = self.view.bounds.height 11 12 // CollectionViewのレイアウトを生成. 13 let layout = UICollectionViewFlowLayout() 14 layout.itemSize = CGSizeMake(width/(CGFloat(gyou)+1),height/CGFloat(retu)+1) //Cellの大きさ 15 layout.sectionInset = UIEdgeInsetsMake(16, 0, 32, 0) // Viewの外枠 cellを表示しない余白のサイズ 16 layout.headerReferenceSize = CGSizeMake(10,10) // セクション毎のヘッダーサイズ. 17 layout.minimumInteritemSpacing = 1.0 //アイテム同士の余白 18 layout.minimumLineSpacing = 2.0 //セクションとアイテムの余白 19 20 // CollectionViewを生成. 21 myCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 22 23 // Cellに使われるクラスを登録. 24 myCollectionView.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 25 26 self.view.addSubview(myCollectionView) 27 28 myCollectionView.delegate = self 29 myCollectionView.dataSource = self 30} 31 32
それでボタンのアクションを
swift
1 @IBAction func HennsyuButton(sender: AnyObject) { 2 retu = 6 3 myCollectionView.reloadData() 4 }
としてreloadさせたいのですがこれではviewdidloadが呼ばれないので見た目が変わらず.なので
次は
swift
1/* 2 Cellに値を設定する 3 */ 4 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 5 let width = self.view.bounds.width 6 let height = self.view.bounds.height 7 let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CustomUICollectionViewCell 8 cell.frame.size = CGSizeMake(width/CGFloat(gyou)+1, height/CGFloat(retu)+1) 9 cell.textLabel?.text = indexPath.row.description 10 print("call here") 11 return cell 12 }
こうしたらloaddata()[ボタンを押すと]でcall here ってでてここが呼ばれたのがわかったのですがcellのサイズが小さくなるだけでcellの表示が6×6になりませんでした.
またcell.frameにCGRectMakeを使ってcellの位置を上のメソッドで変えてやってもサイズだけが変わるだけでcellの位置は変わりませんでした.
ボタンなどで動的にCollectionCellViewの見た目,Cellのサイズ,レイアウトなどを変えることができるようにどのように実装すればよいでしょうか?
またなぜ上のメッソドの中でcellのサイズは変わってもcellの位置は変えられないのでしょうか.お教えお願いします.
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/12 22:29
2015/12/13 01:22