我試圖以編程方式在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
}
另外,我點擊按鈕,只有第一個按鈕顯示並打印出來時0
@IBAction func cellClicked(sender: UIButton) {
print(sender.tag)
}
任何幫助是非常讚賞加一個print語句。
您在'collectionView'數據源中有多少條數據記錄? – Lawliet
我在我當前的測試用例中有兩個數據記錄 – Armand