Ios Swift:collectionviewcell内的操作按钮
我在collectionview单元格中有一个按钮,是我通过编程创建的Ios Swift:collectionviewcell内的操作按钮,ios,swift,uicollectionviewcell,Ios,Swift,Uicollectionviewcell,我在collectionview单元格中有一个按钮,是我通过编程创建的 @IBAction func editButtonTapped() -> Void { print("Hello Edit Button") } j 但是当我点击按钮时,什么也没有发生(它没有显示“Hello Edit button”)。我缺少的是采集视图正在捕获触摸事件并对其进行处理。您需要指定集合视图需要将触摸事件传递给其子类 override func hitTest(_ poin
@IBAction func editButtonTapped() -> Void {
print("Hello Edit Button")
}
j
但是当我点击按钮时,什么也没有发生(它没有显示“Hello Edit button”)。我缺少的是采集视图正在捕获触摸事件并对其进行处理。您需要指定集合视图需要将触摸事件传递给其子类
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
}
有几件事你应该做得不同,但这对我来说很好(基本上就是你发布的代码): 如果可以在集合视图单元格中看到按钮,则代码(假设已正确创建单元格)应已运行 事实上,你也有这样一行:
cell.bringSubview(toFront: editButton)
有点让我觉得你没看到按钮。。。可能是您的
Y
坐标241
将其放置在单元格框架外?您能看到按钮吗?操作函数是在单元格类中还是在集合视图控制器类中?您已将操作的目标指定为“self”,它将是视图控制器。您的方法会给您带来问题,因为将标签存储在单元格中是脆弱的。此外,每次使用单元格时,您似乎都在添加按钮,但可能您只是简化了代码。我建议您使用中的一种方法。您是认真的吗?是的,我是认真的。我现在失明了。非常感谢。241的Y坐标是我需要按钮的位置,我将如何更改单元格框。我尝试在故事板上更改它,但仍然不起作用。您现在如何设置单元格大小?使用约束自动调整其大小?您是否正在实现sizeForItemAt indexPath:
?func collectionView(\uCollectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{返回CGSize(宽度:CGFloat((collectionView.frame.size.width/5)-20),高度:CGFloat(300))}
@DonMag@juelizabeth-嗯,那应该很好。。。您正在将单元格高度设置为300
,因此在Y:241
处放置按钮没有问题。我真的很想知道它的宽度<代码>((collectionView.frame.size.width/5)-20)使单元格非常窄。。。在7+上,纵向为63分
,横向为127分
,但您将按钮宽度设置为154分
?所以问题是你只是没有看到你的按钮?我可以看到按钮完美。当我将按钮设置为Y=41时,它工作正常,可以单击,但在Y=241时,按钮仍然可见,但不可单击
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
}
}
cell.bringSubview(toFront: editButton)