ios - UICollectionViewCell 中归档的可编辑文本阻止调用 didSelectItemAt
问题描述
我UICollectionViewCell
有一个文本字段,当我单击单元格时,它可以让我编辑文本字段,但没有调用didSelectItemAt
函数。UICollectionViewDelegate
我该如何克服呢?
class LetterCell: UICollectionViewCell {
@IBOutlet weak var singleLetterTextField: UITextField!
@IBAction func textDidChange(_ sender: Any) {
if ((singleLetterTextField.text?.count)! > 1) {
singleLetterTextField.text = String((singleLetterTextField.text?.last)!)
}
}
}
这是collectionView函数
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LetterCell", for: indexPath) as! LetterCell
cell.singleLetterTextField.text = data[row][column]
increaseRowColumn()
return cell
}
而且我已经将委托和数据源设置为控制器。
解决方案
- 考虑到您需要可编辑文本字段。
didSelect
如果在文本字段之外触摸单元格,则将起作用。- 这并非不可能,因此如果您想在
didSelect
编辑的同时进行识别,则需要在 textField 中进行计算didBeginEditing
。一个基本的技巧是将索引路径的值设置为标签或您的其他属性textfield
,在cellForItemAt
(例如检查)。您也可以创建自定义文本字段。
这是您的更新cellForItemAt
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LetterCell", for: indexPath) as! LetterCell
cell.singleLetterTextField.text = data[row][column]
cell.singleLetterTextField.tag = indexPath.row//then you can use this tag to form indexPath and with that you can retrieve cell (if it's still visible)
increaseRowColumn()
return cell
}
推荐阅读
- php - PHP:循环遍历多维数组并删除html标签
- embedded - 带有调制解调器 esp8266 中断处理程序的 stm32f 以获得调制解调器应答的结束
- java - Java8:如何在 Hashmap 中使用 Pair
- typescript - 卡在 grafana 中构建面板插件教程
- python - discord.py 我检查是否有人对 wait_for 做出反应
- php - 如何列出我在 Slim Framework 4 中定义到数组中的所有路径?
- python - 我在 tkinter 中使用 messagebox.askokcancel 函数时遇到问题
- c# - 使用反射从方法内部获取“对象”变量字段名称
- javascript - addEventListener 在下拉列表中看不到变化
- node.js - 在我的线性回归模型在 Tensorflow.js 中完成训练后,如何检索系数的值?