问题描述
免责声明:
我已经问过类似的问题,但它们都是指单个按钮。 我有一个collectionView 。在仔细阅读了所有其他问题并尝试将其解决方案组合在一起之后,我问了这个问题。
概述:
我正在使用collectionview创建一个键盘扩展。在我的collectionView中,我有自定义单元格,在类中,我放置了一个按钮。我想为集合视图中的每个按钮添加一个目标。我知道我可以使用selectItemAtIndexRow函数并忽略按钮,但是我需要处理touchUpInside和touchDown事件(因为当我用y键盘键入时,它非常慢),而且我还没有找到使用collectionView单元格的方法(如果有东西让我知道)。为了避免这个问题,我认为最好的解决方案可能是在单元格的类中添加一个按钮并添加所需的操作,但是这样做有很多问题。
我在做什么:
自定义视图类
在这里,我以编程方式创建了我的collectionView。
//Closure to add action to my buttons
var insertLowercase : ((IndexPath) -> ())?
let letters = ["q","w","e","r","t","y","u","i","o","p"]
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
letters.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = keyview.dequeueReusableCell(withReuseIdentifier: "collectionCellId",for: indexPath) as! KeyboardKeys
cell.button.setTitle(letters[indexPath.row],for: .normal)
cell.button.addTarget(self,action: #selector(lettersKeyboard.insert(indexPath:)),for: .touchUpInside)
return cell
}
@objc func insert (indexPath: IndexPath){
insertLowercase?(indexPath)
}
这就是我添加目标的方式。但是,如果按此按钮,键盘将崩溃,并出现以下错误:
线程1:“-[Keyboarddd.KeyboardButton长度]:无法识别的选择器已发送到实例0x7f9bb2506740”
键盘ViewController
在将我的自定义视图放到这里之后,在viewDidLoad中我将其称为:
letters.insertLowercase = { indexPath in
let text = self.letters.letters[indexPath.row]
self.textDocumentProxy.insertText(text)
} //And more stuff to handle quick insertion but this is what you need to reproduce my problem
问题:
如何将目标适当地添加到按钮中?还是有一种方法可以直接使用collectionView单元格做我想做的事情?
解决方法
您的问题是UIButton.addTarget不会调用将IndexPath作为参数的方法。通常,您要做的是处理单元格中的按钮动作,然后在按下按钮时调用回调。
class KeyboardKeys: UICollectionViewCell {
@IBOutlet weak var button: UIButton! {
didSet {
button.addTarget(self,action: #selector(buttonAction(_:)),for: .touchUpInside)
}
}
var didPushButton: (KeyboardKeys) -> Void = { _ in }
@objc func buttonAction(_ button: UIButton) {
didPushButton(self)
}
}
在数据源方法中,您要向每个单元格注册一个回调:
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellId",for: indexPath) as! KeyboardKeys
cell.button.setTitle(letters[indexPath.row],for: .normal)
cell.didPushButton = { [weak self] cell in
guard let indexPath = collectionView.indexPath(for: cell) else { return }
self?.insert(indexPath: indexPath)
}
return cell
}