フォームのように、UITableViewCell
の入力をいくつか入力する必要があるアプリケーションを作成しています。デキューする前にUITableViewCellでユーザー入力を保持する方法
func doneButtonTapped() {
var dict = [String: Any]()
for rows in 0...TableViewCells.getTableViewCell(ceilingType: node.ceilingSelected, moduleType: node.moduleSelected).count {
let ip = IndexPath(row: rows, section: 0)
let cells = tableView.cellForRow(at: ip)
if let numericCell = cells as? NumericInputTableViewCell {
if let text = numericCell.userInputTextField.text {
dict[numericCell.numericTitleLabel.text!] = text
}
} else if let booleanCell = cells as? BooleanInputTableViewCell {
let booleanSelection = booleanCell.booleanToggleSwitch.isOn
dict[booleanCell.booleanTitleLabel.text!] = booleanSelection
}
}
let calculator = Calculator(userInputDictionary: dict, ceiling_type: node.ceilingSelected)
}
:ユーザーが実行ボタンをタップすると、私は別のビューコントローラ
にいくつかの計算と出力それらを実行することができますので、これらの入力を収集する必要がありますここで私はこれらの入力を収集するために使用される方法であります私が抱えている問題は、セルが見えなくなり、ユーザーの入力がメモリからクリアされたときです。あなたが見ることができるように、すべてのセルが表示されたら、
は、行わボタンは、コンソールの印刷から明らかに、ユーザーからのすべての入力を保存するための管理:ここに私のポイントを説明するための2つのスクリーンショットです。細胞は視野の外にある場合には、面積/㎡からの入力はnilに設定されています
心に来た解決策は、私がそうであるように、私がデキュー可能なセルを使用するべきではありませんでした見えないときにセルをメモリに入れたいが、スタックオーバーコミュニティの多くはこのプラクティスに対して強い。この問題をどのように解決すればよいですか?ありがとう!
UPDATE
コードcellForRow(at: IndexPath)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let node = node else {
return UITableViewCell()
}
let cellArray = TableViewCells.getTableViewCell(ceilingType: node.ceilingSelected, moduleType: node.moduleSelected)
switch cellArray[indexPath.row].cellType {
case .numericInput :
let cell = tableView.dequeueReusableCell(withIdentifier: "numericCell", for: indexPath) as! NumericInputTableViewCell
cell.numericTitleLabel.text = cellArray[indexPath.row].title
return cell
case .booleanInput :
let cell = tableView.dequeueReusableCell(withIdentifier: "booleanCell", for: indexPath) as! BooleanInputTableViewCell
cell.booleanTitleLabel.text = cellArray[indexPath.row].title
return cell
}
}
}
私の2つのカスタムセル
NumericInputTableViewCell
class NumericInputTableViewCell: UITableViewCell {
@IBOutlet weak var numericTitleLabel: UILabel!
@IBOutlet weak var userInputTextField: UITextField!
}
BooleanInputTableViewCell
任意の受験者?
あなたが辞書を記入している間に辞書を記入してみませんか?次に、テーブルにではなく辞書にすべてのデータを格納します(推奨されません)。 – GIJOW
デリゲートを使用して、データをdict(データソース)に戻します。セルは再利用されているため、セルの表示データに依存したくありません。 – HMHero
@GIJOWと合意してください。言い換えれば、ビューはデータモデルの代用として使用すべきではありません。 –