Swift:一个collectionviewcell中的Actionbutton
我有一个button,我编程的一个collectionview单元格中创build。
@IBAction func editButtonTapped() -> Void { print("Hello Edit Button") }
Ĵ
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let editButton = UIButton(frame: CGRect(x: 8, y: 241, width: 154, height: 37)) editButton.setImage(UIImage(named: "background-1 (dragged).tiff"), for: UIControlState.normal) editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside) editButton.tag = indexPath.row editButton.isUserInteractionEnabled = true cell.addSubview(editButton) cell.bringSubview(toFront: editButton) }
但是当我点击button,没有任何反应(它不显示“你好编辑button”)。 我错过了什么
有几件事你应该做不同的,但是这对我来说很好(几乎只是你张贴的代码):
class TestCollWithBtnsCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { @IBAction func editButtonTapped() -> Void { print("Hello Edit Button") } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 200.0, height: 200.0) } override func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 8 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) cell.backgroundColor = .orange // this is the WRONG way to do this... // 1) the button is being added avery time a cell is reused // 2) the button should be added to the cell.contentView (not the cell itself) // 3) much better ways to track than setting the button .tag to the row // 4) target for the button action is "self" - locks you into a bad structure // however, this can help us debug the problem let editButton = UIButton(frame: CGRect(x: 8, y: 41, width: 154, height: 37)) // I don't have an image, so I set a button title and background color editButton.setTitle("\(indexPath.row)", for: .normal) editButton.backgroundColor = .red // editButton.setImage(UIImage(named: "background-1 (dragged).tiff"), for: UIControlState.normal) editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside) editButton.tag = indexPath.row editButton.isUserInteractionEnabled = true cell.addSubview(editButton) return cell } }
如果你可以在你的集合视图单元格中看到你的button,那么你的代码(假设你有正确创build的单元格)应该已经工作了。
你也有这样一个事实:
cell.bringSubview(toFront: editButton)
有点让我觉得你没有看到button…可能你的Y
坐标241
放置在单元格外?
收集视图正在捕获触摸事件并对其进行处理。 您需要指定集合视图需要将触摸事件传递给其子类。
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { //loop through the cells, get each cell, and run this test on each cell //you need to define a proper loop for cell in cells { //if the touch was in the cell if cell.frame.contains(point) { //get the cells button for uiview in cell.subviews { if uiview is UIButton { let button = uiview as! UIButton //if the button received the touch, return it, if not, return the cell if button.frame.contains(point) { return button } else { return cell } } } } } //after the loop, if it could not find the touch in one of the cells return the view you are on return view }