前提・実現したいこと
タテヨコ6列、計36個のcollectionviewcellが配置してあるアプリで、設定画面のTableView上のUISwitchをオンにした時に縦列のみ5列に変更する機能をつけたいです。
「セル数、セルのサイズ、セルに対応する数字ラベルの配置変更」が必要になると思います。
(数字ラベル、曜日ラベルの座標はiPhone8を基準に配置しているので、他機種対応できていません...)
ViewControllerの設定ボタンを押すと設定画面に遷移→TableView上にswitchがあり、オンにするとViewControllerのCollectionViewの配置が変わっている という流れにしたいです。
また、1度オンにしたら再度手動でオフにするまでオンのまま保存しておきたいです。
該当のソースコード
Swift
1 2import UIKit 3 4class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 5 6 static var cellNumber: Int = 0 7 @IBOutlet weak var collectionView: UICollectionView! 8 9 static var testArray: [Class] = [] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 collectionView.delegate = self 15 collectionView.dataSource = self 16 17 collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell") 18 19 let mondayLabel = makeLabel(x: 38, text: "月") 20 self.view.addSubview(mondayLabel) 21 22 let tuesdayLabel = makeLabel(x: 97, text: "火") 23 self.view.addSubview(tuesdayLabel) 24 25 let wendnesLabel = makeLabel(x: 158, text: "水") 26 self.view.addSubview(wendnesLabel) 27 28 let thursdayLabel = makeLabel(x: 219, text: "木") 29 self.view.addSubview(thursdayLabel) 30 31 let fridayLabel = makeLabel(x: 278, text: "金") 32 self.view.addSubview(fridayLabel) 33 34 let saturdayLabel = makeLabel(x: 340, text: "土") 35 self.view.addSubview(saturdayLabel) 36 37 let label1 = makeNumLabel(y: 135, text: "1") 38 self.view.addSubview(label1) 39 40 let label2 = makeNumLabel(y: 221, text: "2") 41 self.view.addSubview(label2) 42 43 let label3 = makeNumLabel(y: 307, text: "3") 44 self.view.addSubview(label3) 45 46 let label4 = makeNumLabel(y: 393, text: "4") 47 self.view.addSubview(label4) 48 49 let label5 = makeNumLabel(y: 479, text: "5") 50 self.view.addSubview(label5) 51 52 let label6 = makeNumLabel(y: 568, text: "6") 53 self.view.addSubview(label6) 54 55 } 56 57 58 override func viewWillAppear(_ animated: Bool) { 59 60// 取得 61 if let data = UserDefaults.standard.object(forKey: "TimeTable") as? Data, let array = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Class] { 62 ViewController.testArray = array 63 print("array:(array)") 64 } else { 65 print("Error") 66 } 67 print("testArray:(ViewController.testArray)") 68 collectionView.reloadData() 69 } 70 71 72// セルが選択された時 73 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 74 75 ViewController.cellNumber = indexPath.row 76 77 if ViewController.cellNumber == indexPath.row { 78 print("セル番号:(indexPath.row)") 79 } 80 81 performSegue(withIdentifier: "Modal1", sender: self) 82 } 83 84 85// セルの内容 86 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 87 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell 88 cell.backgroundColor = UIColor.groupTableViewBackground 89 90 if let data = ViewController.testArray.first(where: { $0.cellNumber == indexPath.row }) { 91 cell.classLabel.text = data.className 92 cell.roomLabel.text = data.roomName 93 } else { 94 cell.classLabel.text = "" 95 cell.roomLabel.text = "" 96 } 97 return cell 98 } 99 100// セクション数 101 func numberOfSections(in collectionView: UICollectionView) -> Int { 102 return 1 103 } 104 105// セル数 106 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 107 108 return 36 109 } 110 111// セルのサイズ 112 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 113 let cellSize = CGSize(width: collectionView.frame.width / 6 - 1, height: collectionView.frame.height / 6 - 1) 114 115 return cellSize 116 } 117 // 余白 118 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 119 return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 120 } 121 122 // 行の余白 123 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 124 return 1 125 } 126 127 // 列の余白 128 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 129 return 1 130 } 131 132// 曜日ラベル 133 func makeLabel(x: CGFloat,text: String) -> UILabel { 134 let label = UILabel(frame: CGRect(x: x, y: 78, width: 18, height: 21)) 135 label.text = text 136 137 return label 138 } 139// 数字ラベル 140 func makeNumLabel(y: CGFloat, text: String) -> UILabel { 141 let label = UILabel(frame: CGRect(x: 2, y: y, width: 20, height: 27)) 142 label.text = text 143 144 return label 145 } 146 147//設定画面に遷移 148 @IBAction func settingBtn(_ sender: UIButton) { 149 150 performSegue(withIdentifier: "settingSegue", sender: nil) 151 } 152
Swift
1 2//設定画面 3 4import UIKit 5 6class TableViewController: UITableViewController { 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 12 } 13 14 override func didReceiveMemoryWarning() { 15 super.didReceiveMemoryWarning() 16 } 17 18 19 override func numberOfSections(in tableView: UITableView) -> Int { 20 return 1 21 } 22 23 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 24 return 2 25 } 26
Swift
1 2import UIKit 3 4class TableViewCell: UITableViewCell { 5 6 @IBOutlet weak var numberLabel: UILabel! 7 @IBOutlet weak var numberSwitch: UISwitch! 8 9 override func awakeFromNib() { 10 super.awakeFromNib() 11 } 12 13 override func setSelected(_ selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 16 } 17 18} 19
回答1件
あなたの回答
tips
プレビュー