解決したいこと
試したこと
ContentsTableViewCell.xibで高さ、横幅を決めた
ContentsCollectionViewCell.xibでも同じように高さ、横幅を決めた
コード(ContentsTableViewCell)
import UIKit class ContentsTableViewCell: UITableViewCell { @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var contentsCollectionView: UICollectionView! var collectionViewOffset: CGFloat { get { return contentsCollectionView.contentOffset.x } set { contentsCollectionView.contentOffset.x = newValue } } override func awakeFromNib() { super.awakeFromNib() let nib = UINib(nibName: "ContentsCollectionViewCell", bundle: .main) contentsCollectionView.register(nib, forCellWithReuseIdentifier: "ContentsCollectionViewCell") } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func setCollectionViewDataSourceDelegate <D: UICollectionViewDataSource & UICollectionViewDelegate> (dataSourceDelegate: D, forRow row: Int) { contentsCollectionView.delegate = dataSourceDelegate contentsCollectionView.dataSource = dataSourceDelegate contentsCollectionView.reloadData() } }
コード(ContentsCollectionViewCell側)
import UIKit class ContentsCollectionViewCell: UICollectionViewCell { @IBOutlet weak var contentsImageView: UIImageView! override func awakeFromNib() { super.awakeFromNib() configure() } func configure() { contentsImageView.layer.cornerRadius = 20 contentsImageView.layer.masksToBounds = true contentsImageView.image = UIImage(named: "image-1") } }
コード(ViewController側)
import UIKit class ViewController: UIViewController { @IBOutlet weak var contentsTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() configureUI() } func configureUI() { contentsTableView.delegate = self contentsTableView.dataSource = self let nib = UINib(nibName: "ContentsTableViewCell", bundle: .main) contentsTableView.register(nib, forCellReuseIdentifier: "ContentsTableViewCell") contentsTableView.tableFooterView = UIView() } } extension ViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 200 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { guard let cell = cell as? ContentsTableViewCell else { return } cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.section) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ContentsTableViewCell", for: indexPath) as! ContentsTableViewCell cell.backgroundColor = UIColor.init(red: 12/255, green: 40/255, blue: 96/255, alpha: 1) cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) cell.contentsCollectionView.reloadData() return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } } extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentsCollectionViewCell", for: indexPath) as! ContentsCollectionViewCell cell.layer.shadowOffset = CGSize(width: 0, height: 0) cell.layer.shadowColor = UIColor.init(red: 183/255, green: 223/225, blue: 236/255, alpha: 1).cgColor cell.layer.shadowOpacity = 3.0 cell.layer.shadowRadius = 5 return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 150, height: 150) } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/10 13:38