###実現したいこと
Swift3のUICollectionViewのCellサイズを動的に変更したいです。
現在はセルをタップしたら「わ」を追加して、
リロードボタンを押した時にセルの表示を更新したいのですが、
セルのサイズがよく分からないサイズになってしまい、正しく表示がされないです。詰めて表示もされない状態です。
reloadDataの関係なのか、CollectionViewが悪いのか。
お手上げなので、ご教示いただけると幸いです。
ゆくゆくは1列の横スクロールにして、Cellの追加、変更削除ができるようにしたいと思っています。
###該当のソースコード
Swift
1// 2// ViewController.swift 3// UIKit054_3.0 4// 5// Created by KimikoWatanabe on 2016/08/21. 6// Copyright © 2016年 FaBo, Inc. All rights reserved. 7// 8 9import UIKit 10 11class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UIToolbarDelegate { 12 13 var array = ["あ","いい","ううう","ええええ","おおおおお","かかかかかか","ききききききき"] 14 var myCollectionView : UICollectionView! 15 private var myToolbar: UIToolbar! 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 // CollectionViewのレイアウトを生成. 20 let layout = UICollectionViewFlowLayout() 21 22 // Cell一つ一つの大きさ. 23 layout.itemSize = CGSize(width:50, height:50) 24 25 // Cellのマージン. 26 layout.sectionInset = UIEdgeInsetsMake(16, 16, 32, 16) 27 28 // セクション毎のヘッダーサイズ. 29 layout.headerReferenceSize = CGSize(width:100,height:30) 30 31 // CollectionViewを生成. 32 myCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 33 34 // Cellに使われるクラスを登録. 35 myCollectionView.register(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 36 37 myCollectionView.delegate = self 38 myCollectionView.dataSource = self 39 40 self.view.addSubview(myCollectionView) 41 42 // ツールバーのサイズを決める. 43 myToolbar = UIToolbar(frame: CGRect(x:0, y:self.view.bounds.size.height - 44, width:self.view.bounds.size.width, height:40.0)) 44 45 // ツールバーの位置を決める. 46 myToolbar.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height-20.0) 47 48 // ツールバーの色を決める. 49 myToolbar.barStyle = .blackTranslucent 50 myToolbar.tintColor = UIColor.white 51 myToolbar.backgroundColor = UIColor.black 52 53 // ボタン1を生成する. 54 let myUIBarButtonGreen: UIBarButtonItem = UIBarButtonItem(title: "リロード", style:.plain, target: self, action: #selector(ViewController.onClickBarButton(sender:))) 55 myUIBarButtonGreen.tag = 1 56 57 // ボタンをツールバーに入れる. 58 myToolbar.items = [myUIBarButtonGreen] 59 60 // ツールバーに追加する. 61 self.view.addSubview(myToolbar) 62 } 63 /* 64 Cellが選択された際に呼び出される 65 */ 66 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 67 // 押したセルに「わ」を追加する 68 array[indexPath.row] = array[indexPath.row] + "わ" 69 } 70 71 /* 72 Cellの総数を返す 73 */ 74 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 75 return array.count 76 } 77 78 /* 79 Cellに値を設定する 80 */ 81 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 82 83 let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CustomUICollectionViewCell 84 cell.textLabel?.text = array[indexPath.row].description 85 return cell 86 } 87 88 // Cellのサイズを設定する 89 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 90 let str = array[indexPath.row].characters.count * 20 91 let returnSize = CGSize(width: str, height: 50) 92 return returnSize 93 } 94 95 /* 96 UIBarButtonItemが押された際に呼ばれる. 97 */ 98 internal func onClickBarButton(sender: UIBarButtonItem) { 99 myCollectionView.updateConstraintsIfNeeded() 100 myCollectionView.reloadData() 101 myCollectionView.layoutIfNeeded() 102 } 103} 104 105
Swift
1class CustomUICollectionViewCell : UICollectionViewCell{ 2 3 var textLabel : UILabel? 4 5 required init(coder aDecoder: NSCoder) { 6 super.init(coder: aDecoder)! 7 } 8 9 override init(frame: CGRect) { 10 super.init(frame: frame) 11 12 // UILabelを生成. 13 textLabel = UILabel(frame: CGRect(x:0, y:0, width:frame.width, height:frame.height)) 14 textLabel?.text = "nil" 15 textLabel?.backgroundColor = UIColor.white 16 textLabel?.textAlignment = NSTextAlignment.center 17 18 // Cellに追加. 19 self.contentView.addSubview(textLabel!) 20 } 21 22}
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3、Xcode 8.2.1
・参考サイト
054 UICollectionViewのCellをカスタマイズする
あなたの回答
tips
プレビュー