いくつかのサンプルコードを提供していないので、私はここで多くの仮定を行います。
あなたはUITableView
その中
class CalculatorViewController
@IBOutlet var tableView: UITableView!
var values: [Double] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}
を持ってUIViewController
を使用している今、あなたは基本的なのViewControllerを持っていますが、コンパイラがCalculatorViewControllerがUITableViewDataSource
とUITableViewDelegate
に準拠していないと言うだろうと言うことができます。さんは
extension CalculatorViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
// return your number of sections, let say it's one
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Let's say you only have 3 cells at the moment
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "YourCustomInputFieldCell") as! YourCustomInputFieldCell
return cell
}
}
はのは、ここでも、コンパイラはCalculatorViewController
がUITextFieldDelegate
に準拠していないと文句を言うだろうUITableViewDelegate
エラー
extension CalculatorViewController: UITableViewDelegate {
// This one gets called each time a cell will be displayed (as it says)
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let cell = cell as? YourCustomInputTextField {
// I assume that you expose your cell's input field
// By setting a tag on the input field you can
// distinguish it from other inputs
cell.input.tag = indexPath.row
cell.input.delegate = self
}
}
}
を修正しましょうということ修正しましょう。それも修正しましょう。
extension CalculatorViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Here you can check the tag of the textField
// and update the values array accordingly
// You should probably convert the string that you get to
// the number format that you want
return true
}
}
希望します。
テーブルビューにはいくつのUITextFieldがありますか?どのように見えるのか、tableviewのスクリーンショットを共有できますか? –
ビューではなくデータを操作する場合は、セル内のテキストフィールドに何かを入力してすぐにデータソースの 'endEditting'に保存してから値を取得してください。 – Tj3n
コントローラクラスの私は通常、UITableViewを読み書きできる配列でバックアップします。配列インデックスとUITableViewCellインデックスが一致するので、配列内の項目に対してUITableViewの各行を識別できます。 – aorticDefiance