gpt4 book ai didi

ios - UITableView 在文本字段开始编辑时自动滚动

转载 作者:行者123 更新时间:2023-11-28 13:47:12 25 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,它具有不同的形式,根据用户的选择填充到 UITableview 中。每个表单都有不同的字段,如 Textfield、DatePicker、PickerView。所以我使用一个 TableViewCell (nib) 来保存所有这些并根据问题显示或隐藏项目。定义了保存函数,当用户输入数组时将保存值。

我的问题是,有时我的 tableview 会滚动,就像索引失控一样。就像当我选择任何文本字段时,Tableview 会滚动到顶部。我已经删除了所有键盘观察器方法并进行了检查,它仍然在发生。

下面是我的保存函数代码:

 func saveFormValue(mystr: String) {
//selectedIndex is a global variable (NSIndexPath)
let dict = sections[selectedIndex!.section].questions![selectedIndex!.row]
dict.answer = mystr
sections[selectedIndex!.section].questions![selectedIndex!.row] = dict
let answer = updated_answer.init(ID: ID, form_id: selected_form_id, form_name: formName, section_id: dict.section_id ?? "",question_id: dict.question_id ?? "", question_name: dict.question_name!,question_type:dict.question_type!)

updatedArray.append(answer)
self.tableView.reloadData()
}

这是 textfieldDidBeginEditing 函数中的代码(selectedIndexPath 是如何初始化的):

 guard let index = tableView.indexPath(for: cell) else {
return
}
selectedIndex = index as NSIndexPath

我为单元格添加了委托(delegate),我注意到的一件事是,每当我按 pickerview 或 datepicker 一次时,就会发生此问题。如果我只触摸 textField 单元格,我看不到这个问题。

请让我知道任何进一步的细节。

最佳答案

试试这段代码希望这对你有帮助。

 if let thisIndexPath = tableView.indexPath(for: cell) {
tableView.scrollToRow(at: thisIndexPath, at: .top, animated: false)
}

关于ios - UITableView 在文本字段开始编辑时自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com