【问题标题】:Need to edit a UITextView inside an expanding UITableViewCell需要在扩展的 UITableViewCell 中编辑 UITextView
【发布时间】:2018-10-24 19:59:43
【问题描述】:

我在 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 中可以使用的任何触摸方法也必须在应用程序中没有这些扩展功能的其他单元格中使用。因此,如果我可以使用另一种技术来扩展它会更干净。

【问题讨论】:

    标签: swift uitableview uitextview


    【解决方案1】:

    我通过将 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()
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 2011-09-13
      • 2012-04-30
      相关资源
      最近更新 更多