【发布时间】:2017-12-14 22:15:25
【问题描述】:
我正在尝试以编程方式在 UICollectionView 的每个单元格中创建一个按钮;但是,只有第一个按钮可见。我尝试添加打印语句以查看我的单元格有哪些子视图,并且按钮存在但它没有出现在屏幕上。
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)
}
非常感谢任何帮助。
【问题讨论】:
-
collectionView数据源中有多少条数据记录? -
我当前的测试用例中有两条数据记录
标签: ios swift uibutton uicollectionview uicollectionviewcell