2016-11-15 3 views
0

私は計算を実行するために電卓のUIButtonをクリックする前に変数を入力できるようにするUITableViewを含む電卓アプリケーションを作っています。なぜなら、UITableFieldsを計画するのではなく、UITableViewを使用する理由は、私のアプリにはさまざまな計算が含まれているため、ユーザーの選択に基づいてUITableViewCellsの数が変化するからです。UITableViewのUITableViewCellsのUITextFieldsからテキストを取得し、配列(Swift 3)に配置する方法はありますか?

電卓のUIButtonが押されるたびに、表示可能なUITextField値の配列を取得する方法を見つけることを望んでいました。 UITableViewクラスは、取得するのではなく、セルにデータを入力する方がより適しているようです。私が使用できる別のアプローチはありますか?私はスウィフト3ファイルで1つのストーリーボードを使用しています。

+1

テーブルビューにはいくつのUITextFieldがありますか?どのように見えるのか、tableviewのスクリーンショットを共有できますか? –

+0

ビューではなくデータを操作する場合は、セル内のテキストフィールドに何かを入力してすぐにデータソースの 'endEditting'に保存してから値を取得してください。 – Tj3n

+0

コントローラクラスの私は通常、UITableViewを読み書きできる配列でバックアップします。配列インデックスとUITableViewCellインデックスが一致するので、配列内の項目に対してUITableViewの各行を識別できます。 – aorticDefiance

答えて

1

いくつかのサンプルコードを提供していないので、私はここで多くの仮定を行います。

あなたはUITableView

その中
class CalculatorViewController 
    @IBOutlet var tableView: UITableView! 
    var values: [Double] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.dataSource = self 
     tableView.delegate = self 
    } 
} 

を持ってUIViewControllerを使用している今、あなたは基本的なのViewControllerを持っていますが、コンパイラがCalculatorViewControllerがUITableViewDataSourceUITableViewDelegateに準拠していないと言うだろうと言うことができます。さんは

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 
    } 

} 

はのは、ここでも、コンパイラはCalculatorViewControllerUITextFieldDelegateに準拠していないと文句を言うだろう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 
    } 
} 

希望します。

+0

有益な答えをありがとう。 UITextFieldDelegateは私が欠けていたビットでした。 – Kalchi25

+0

@ Kalchi25ようこそ!それがあなたを助けたら答えを受け入れるように自由に感じてください – gkaimakas

関連する問題