Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何更新表视图单元格';不调用tableView.reloadData()的textView标记_Ios_Swift_Uitableview_Callback_Textview - Fatal编程技术网

Ios 如何更新表视图单元格';不调用tableView.reloadData()的textView标记

Ios 如何更新表视图单元格';不调用tableView.reloadData()的textView标记,ios,swift,uitableview,callback,textview,Ios,Swift,Uitableview,Callback,Textview,我有一个tableView控制器,它利用带有文本视图的自定义单元格。目前,我有一个回调函数,这样每当用户在编辑textView时点击return时,就会在tableView中插入一个新的单元格,它就会成为第一个响应者。为了做到这一点,每当用户点击return时,我都将textView的标记+1存储在一个名为cellCreatedWithReturn的变量中,这样tableView就知道在哪里插入单元格,以及使用哪个单元格作为第一响应程序。问题是,在tableView重新加载之前,会出现重复的te

我有一个tableView控制器,它利用带有文本视图的自定义单元格。目前,我有一个回调函数,这样每当用户在编辑textView时点击return时,就会在tableView中插入一个新的单元格,它就会成为第一个响应者。为了做到这一点,每当用户点击return时,我都将textView的标记+1存储在一个名为cellCreatedWithReturn的变量中,这样tableView就知道在哪里插入单元格,以及使用哪个单元格作为第一响应程序。问题是,在tableView重新加载之前,会出现重复的textView标记。如果已经有一个标记为5的textView,并且我在编辑标记为4的textView时点击了return,它将创建另一个标记为5的textView单元格。现在,5的第一个标记位于第6行,但由于它被标记为5,因此在其上单击return将导致在第6行而不是第7行插入单元格。我尝试将回调更改为只使用indexPath.row而不是cellCreatedWithReturn变量,但这会导致新单元格始终作为tableView中的最后一个单元格插入。我不想调用tableView.reloadData(),因为这会导致在重新分配第一个响应者时,键盘会被降下,然后立即升起,这看起来不太好。我需要键盘保持抬高

以下是调用回调的shouldChangeTextIn:

var cellCreatedWithReturn: Int?
var returnKeyCallback: (()->())?

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if(text == "\n") {
        cellCreatedWithReturn = textView.tag + 1
        print(cellCreatedWithReturn!)
        returnKeyCallback?()
        return false
    } else {
        return true
    }
}
以及cellForRowAt中的回调函数:

self.returnKeyCallback = { [weak self] in
    if let self = self {
        let newRow = self.cellCreatedWithReturn! - 1
        // this is -1 because cell 0 is not included in the same data source
        do {
            try self.realm.write {
                self.song.lyrics.insert(LyricLine(), at: newRow)
            }
        } catch {
            print("Error when inserting new lyric line to song in Realm when return pressed")
        }
        let newIndexPath = IndexPath(row: self.cellCreatedWithReturn!, section: 0)
        print(newIndexPath.row)
        self.tableView.performBatchUpdates({
            self.tableView.insertRows(at: [newIndexPath], with: .automatic)
        }, completion: { b in
            guard let c = tableView.cellForRow(at: newIndexPath) as? newNoteTableViewCell else { return }
            c.lyricsField.becomeFirstResponder()
            c.lyricsField.tag = self.cellCreatedWithReturn!
            if indexPath.row > newIndexPath.row {
                cell.lyricsField.tag += 1
            }
        })
    }
}

不必将行信息存储在textview的标记中,您可以获取当前选定单元格的索引路径,然后在其下方添加一个新单元格,然后选择该新单元格(以及其中的文本字段)@andreasetjen这种方法的问题是,点击单元格编辑其textview不会选择该单元格。我知道。您需要(通过编程)选择保存文本视图的单元格。@AndreasOetjen我明白了。在这种情况下该怎么做?谢谢你的帮助,我知道了。我需要先在UITextView上禁用用户交互,然后在选择单元格时以编程方式启用它,并在下一行中分配第一个响应者。