質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
タグ

特殊な記法により文書に埋め込む形で記述される付加情報をタグと呼びます。文書構造や書式、文字飾りなどを指示したり、画像や他の文書へのリンクを埋め込むことができる。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

2614閲覧

Swift3のUICollectionViewのCellサイズを動的に変更したい。

kirby_kino

総合スコア8

タグ

特殊な記法により文書に埋め込む形で記述される付加情報をタグと呼びます。文書構造や書式、文字飾りなどを指示したり、画像や他の文書へのリンクを埋め込むことができる。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2017/04/26 07:13

###実現したいこと
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をカスタマイズする

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問