首页 > 解决方案 > 所以我想找到刚刚编辑过的单元格的 indexPath.row,也就是在 func textViewDidEndEditing 内部

问题描述

更新:好的,所以我尝试使用重复问题中建议的代码,但似乎对我不起作用,因为我之前已经明确表示我想访问 cellForRowAt 函数之外的 indexPath.row ,但不能访问它通过自定义单元格,因为我需要 tableViewController 中的 UITextView 委托来更新 textViewDidChange 上的 tableView 以便自动调整单元格大小。

我试图编写这段代码,并且我已经写了关于当我用控制台检查它时每个地方会发生什么的笔记。生病张贴下面的代码。

如前所述,我想从用户输入中保存文本,并在 indexPath.row = 文本输入行后将其删除。

 //if endEditing, save text to array cellNumber, at the point inside the array, equalavant of the indexPath.row
func textViewDidEndEditing(_ textView: UITextView){

    //get position of current row
    var position: CGPoint = self.tableView.convert(CGPoint.zero, to: self.tableView) // always returns 0.0 0.0 pos

    if let indexPath = self.tableView.indexPathForRow(at: position){ //returns 0, 0 always 

        let cellIndexPathRow = indexPath.row // problem: always comes back as 0, should return indexPath.row
        print(cellIndexPathRow)
        cellNumber.insert(textView.text, at: cellIndexPathRow)}

}

标签: swifttableview

解决方案


您可以尝试像这样访问单元格

func textViewDidEndEditing(_ textView: UITextView){
    if let cells = self.tableView.visibleCells as? [CustomCellClass], let cell = cells.filter({$0.textView == textView}).first {
        // Do what you want with the cell here
    }
}

推荐阅读