swift - 需要在扩展的 UITableViewCell 中编辑 UITextView
问题描述
我在 TableView 中有一个动态单元格,它会根据 TextView 中的信息自动调整大小。这发生在 tableView: didSelectRowAt 函数中。简化版如下:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? cell else { return }
if cell.textViewOutlet.attributedText.length != 12 {
cell.textViewOutlet.attributedText = NSMutableAttributedString(
string: "Expand:\n\n\n " + String(indexPath.row)
)
} else {
cell.textViewOutlet.attributedText = NSMutableAttributedString(
string: "Cell: " + String(indexPath.row)
)
}
在图像中,单元格 1 被展开,单元格 0 和 2 被压缩。使用此设置,我可以让用户通过选择单元格在展开和压缩单元格(和消息)之间切换。
我的问题是我想让用户能够编辑文本。当然,当用户点击编辑时,“didSelectRowAt”函数就进入了。
区分编辑操作和展开操作的最佳方法是什么?我可以使动作成为多点触控或长点触控动作吗?
一个限制是,在 textView 中可以使用的任何触摸方法也必须在应用程序中没有这些扩展功能的其他单元格中使用。因此,如果我可以使用另一种技术来扩展它会更干净。
解决方案
我通过用捏手势替换 didSelectRowAt 动作来扩展或收缩单元格来解决这个问题。因此,textView 编辑可以正常进行。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
...
let pinch = UIPinchGestureRecognizer(target: self,
action: #selector(pinchResponse(recognizer:)))
cell.addGestureRecognizer(pinch)
cell.isUserInteractionEnabled = true
return cell
}
@objc func pinchResponse(recognizer: UIPinchGestureRecognizer) {
print("in pinchResponse recognizer.scale: \(recognizer.scale)")
if recognizer.state == UIGestureRecognizer.State.ended {
let pinchLocation = recognizer.location(in: tableView)
if let pinchIndexPath = tableView.indexPathForRow(at: pinchLocation) {
if let pinchedCell = tableView.cellForRow(at: pinchIndexPath) as? StepTableViewCell {
// 2
if recognizer.scale > 1.0 {
print("Expand pinchIndexPart.count: \(pinchIndexPath.count)")
// Add expand actions
print("pinchedCell: \(String(describing: pinchedCell.recipeNbrLabel.text))")
} else {
print("Contract pinchIndexPart.count: \(pinchIndexPath.count)")
// Add contract actions
print("pinchedCell: \(String(describing: pinchedCell.recipeNbrLabel.text))")
}
tableUpdate()
}
}
}
}
推荐阅读
- html - 变量插入闪烁功能
- hive - HIve:表创建失败并显示“由于 OWN_TASK_FAILURE 导致顶点未成功”
- json - Scala Play:将大型 JSON 模板外部化的方法是什么?
- python - 如何使 scipy.optimize.curve_fit 产生更好的正弦回归拟合?
- javascript - javascript window.open 没有为不同的网址打开不同的窗口?
- jquery - 图像相乘
- html - HTML a href 目标设置为“_blank”而不移动到打开的页面
- keras - AttributeError:“模块”对象在将 Keras 转换为 Core ML 模型时没有属性“SeparableConv1D”
- unit-testing - 如何恢复使用 jest.mock() 创建的模拟?
- php - 在单元测试时包括问题