前提・実現したいこと
ViewController
にTableView
を配置し、
TableViewCell
上にCollectionView
を配置しています。
画面の横幅を基準とし、5等分にしたものをCollectionViewCell
の横幅とし、
隙間なく配置する画面を想定しています。
しかし処理の完成後に実行すると、表示直後はうまく表示されず、
TableView
をスクロールし、TableViewCell
が再生成されると、正しいサイズで表示されます。
こちらを表示直後から正しい形で表示したいのですが、実装方法が思いつきませんでした。
該当のソースコード
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var sampleTableView: UITableView! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 self.sampleTableView.delegate = self 11 self.sampleTableView.dataSource = self 12 13 let nib = UINib(nibName: "SampleTableViewCell", bundle: nil) 14 self.sampleTableView.register(nib, forCellReuseIdentifier: "Cell") 15 } 16 17 override func viewWillAppear(_ animated: Bool) { 18 super.viewWillAppear(animated) 19 20 self.sampleTableView.reloadData() // 追加 21 } 22 23} 24 25extension ViewController: UITableViewDelegate, UITableViewDataSource { 26 27 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 28 return 5 29 } 30 31 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 32 return UIScreen.main.bounds.width / 5.0 * 6.0 33 } 34 35 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 36 guard let cell = cell as? SampleTableViewCell else { return } 37 cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) 38 } 39 40 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 41 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SampleTableViewCell 42 return cell 43 } 44 45} 46 47extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { 48 49 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 50 return 30 51 } 52 53 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 54 print(indexPath.row) 55 } 56 57 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 58 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! SampleCollectionViewCell 59 print(cell.frame.size) 60 return cell 61 } 62 63}
Swift
1import UIKit 2 3class SampleTableViewCell: UITableViewCell { 4 5 @IBOutlet weak var sampleCollectionView: UICollectionView! 6 7 override func awakeFromNib() { 8 super.awakeFromNib() 9 10 let nib = UINib(nibName: "SampleCollectionViewCell", bundle: nil) 11 self.sampleCollectionView.register(nib, forCellWithReuseIdentifier: "Cell") 12 } 13 14 func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(dataSourceDelegate: D, forRow row: Int) { 15 let layout = UICollectionViewFlowLayout() 16 layout.minimumLineSpacing = 0.0 17 layout.minimumInteritemSpacing = 0.0 18 layout.itemSize = CGSize(width: self.sampleCollectionView.frame.width / 5, height: self.sampleCollectionView.frame.width / 5) 19 20 print(self.sampleCollectionView.frame) // 追加 21 22 self.sampleCollectionView.delegate = dataSourceDelegate 23 self.sampleCollectionView.dataSource = dataSourceDelegate 24 self.sampleCollectionView.collectionViewLayout = layout 25 self.sampleCollectionView.reloadData() 26 } 27 28}
Swift
1 2import UIKit 3 4class SampleCollectionViewCell: UICollectionViewCell { 5 6 override func awakeFromNib() { 7 super.awakeFromNib() 8 // Initialization code 9 } 10 11}
実際の画面
あなたの回答
tips
プレビュー