Swift 需要在扩展的UITableViewCell中编辑UITextView
我在TableView中有一个动态单元格,可以根据TextView中的信息自动调整大小。这发生在tableView:didSelectRowAt函数中。简化版本如下:Swift 需要在扩展的UITableViewCell中编辑UITextView,swift,uitableview,uitextview,Swift,Uitableview,Uitextview,我在TableView中有一个动态单元格,可以根据TextView中的信息自动调整大小。这发生在tableView:didSelectRowAt函数中。简化版本如下: override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let cell = tableView.cellForRow(at: indexPath) as? cell else { ret
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()
}
}
}
}