2016-11-09 6 views
-1

私は迅速です。私は1つのカスタムUITableViewCellを持つUITableViewを持っています。 1つのUITextフィールドがあります。デフォルトでnumberOfRowsは1です。セル内のテキストフィールドに入力された値に基づいてテーブルビューセルを動的に増加させます。

このテキストフィールドに整数値(x)を入力してEnterキーを押すと、tableviewcellにx個のtableviewcellが追加され、すべてのセルにテキストフィールドが追加されます。ここで

は私のコードです:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell : TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell 
    cell.textField?.text = "Created" 
    cell.textField.tag = indexPath.row 
    cell.textField.addTarget(self, action: #selector(textFieldShouldReturn()), forControlEvents: <#T##UIControlEvents#>) 

    return cell 
} 
+0

のような最初のものとは異なる細胞をしたい場合には、あなたは、行の検証を持つことができます。 「n値を入力した」と「n回増やす」とはどういう意味ですか? – ganzogo

答えて

0

あなたはそれがあなたの上のレコード数をインクリメントした場合、ユーザは、挿入された値が有効な数値であるかどうかを確認するために、編集を終了したときIBActionを設定したいものを達成するにはテーブルをリフレッシュするにはtableView.reloadData()を呼び出します。あなたのcellForRowAtIndexPathで

あなたはあなたが求めているものを私に明確ではないので、

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: UITableViewCell! 

    if indexPath.row == 0 { 
     cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell 
     cell.textField?.text = "Created" 
     cell.textField.tag = indexPath.row 
     UITextField().addTarget(self, action: #selector(customMethod), for: .editingDidEnd) 
    } else { 
     cell = self.tableView.dequeueReusableCellWithIdentifier("otherCell") as! UITableViewCell 
     cell.textLabel?.text = "Created programatically" 
    } 

    return cell 
} 
関連する問題