UICollectionViewの各セルにボタンをプログラムで作成しようとしています。ただし、最初のボタンだけが表示されます。私は印刷文を追加して、私のセルが持っているサブビューを見てみました。ボタンは存在しますが、画面には表示されません。ボタンをクリックすると複数のボタンがUICollectionViewCellに表示されない
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
// Configure the cell
let button = UIButton(frame: cell.frame)
button.addTarget(self, action: #selector(cellClicked), for: UIControlEvents.touchUpInside)
button.backgroundColor = UIColor.red
button.tag = indexPath.row
cell.addSubview(button)
print(cell.subviews)
return cell
}
はまた、私はprint文を追加し、最初のボタンは、すべてのヘルプははるかに高く評価されるまで表示され、0
@IBAction func cellClicked(sender: UIButton) {
print(sender.tag)
}
を出力します。
あなたの 'collectionView'データソースにはいくつのデータレコードがありますか? – Lawliet
私は現在のテストケースに2つのデータレコードを持っています – Armand