我在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中工作的任何触摸方法都必须在应用程序上没有这些扩展功能的其他单元中使用。因此,如果我可以使用另一种技术来扩展,它将会更清晰。
发布于 2018-11-18 22:54:35
我解决了这个问题,方法是用一个收缩手势替换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()
}
}
}
}https://stackoverflow.com/questions/52977072
复制相似问题