たとえば、私は2つのテキストフィールドと1つのラベルを持つセルを持つUITableViewを持っています。ラベルにはテキストフィールドの数値の合計が表示されるため、ユーザーがtextFieldを編集するたびにラベルが更新されます。問題は、私が直面しているのは、ユーザーがtextFieldをタイプするたびに、私はtableView.reloadData()関数を呼び出して最新の合計でラベルを更新し、この結果textFieldを辞めることになり、キーボードが隠れます。私がしたいのは、開いているキーボードでラベルを更新することです。どうすればこれを達成できますか?サンプルコードは次のとおりです。update UITextfieldのテキストを持つUITableviewセル
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.textField1.tag = indexPath.row
cell.textField2.tag = indexPath.row
cell.textField1.addTarget(self, action: #selector(textField1Edited), for: .editingChanged)
cell.textField2.addTarget(self, action: #selector(textField2Edited), for: .editingChanged)
cell.sumLabel.text = "\(textField1Values[indexPath.row]+textField2Values[indexPath.row])"
return cell
}
@objc func textField1Edited(sender: UITextField!) {
textField1Values[sender.tag] = sender.text
self.tableView.reloadData()
}
@objc func textField2Edited(sender: UITextField!) {
textField2Values[sender.tag] = sender.text
self.tableView.reloadData()
}
ありがとうございます。
あなたは、いくつかのデモコードを共有してくださいすることができますか?それは非常に役に立ちます –
@ danishnaeem、あなたのコードを投稿することができます、私はそれを変更して提供することができます。 jzk – ahmed
コードで更新された回答を参照してください。 –