import UIKit class ViewController: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func numberOfSections(in collectionView: UICollectionView) -> Int { return numbers.count } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return numbers[section].count } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return .init(width: collectionView.frame.width, height: 10) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width = (collectionView.frame.width - 3 * 10) / 4 return .init(width: width, height: width) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = calculaterCollectionView.dequeueReusableCell(withReuseIdentifier: "cellld", for: indexPath) as! CalculatorViewCell // cell.backgroundColor = .blue return cell } let numbers = [ ["1","2","3","4"], ["1","2","3","4"], ["1","2","3","4"], ["1","2","3","4"], ["1","2","3","4"], ] @IBOutlet weak var numberLabel: UILabel! @IBOutlet weak var calculaterCollectionView: UICollectionView! @IBOutlet weak var calculatarHeightConstraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() calculaterCollectionView.delegate = self calculaterCollectionView.dataSource = self calculaterCollectionView.register(CalculatorViewCell.self, forCellWithReuseIdentifier: "cellld") calculatarHeightConstraint.constant = view.frame.width * 1.4 // Do any additional setup after loading the view. } class CalculatorViewCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .black } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } }
Xcode初心者です。
電卓のアプリを作りたいと思っています。
↑のコードを実行したらCalculater Collection Viewがしろくなります。
解決方法を教えて欲しいです。
試しに動かしてみたら、ちゃんと黒くなりましたよ。
あなたの回答
tips
プレビュー