2016-07-06 27 views
0

私の質問は、特に以下の一連のアクションに関連しています。ユーザーがtextFieldにテキストを入力し、ボタンを押してからテキストを入力できるようにしたいセルに転送された後、テーブルに新たに移入されます。TextFieldテキストを含むセルを追加するボタンXcode/Swift 2.0

私は、テキストフィールド、ボタン、およびテーブルオブジェクトを持つビューコントローラを1つ持っています。

UPDATE 1: "self.taskTable.delegate = self"と "self.taskTable.dataSource = self"が追加されました。これは、ユーザーがテキストを入力するときに各文字列を格納する配列を持っています。コメントに示唆されているように。

更新2:ストーリーボードエディタで識別子を追加するのを忘れました。コードは機能しています、ありがとう皆さん!次のようにコードがある:あなたのviewDidLoad()

にデリゲートの参照を追加しなかった

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
@IBOutlet var textField: UITextField! 
@IBOutlet var taskTable: UITableView! 
var taskArray : NSMutableArray! = NSMutableArray() //Could be a non-NS as well 

@IBAction func addTask(sender: AnyObject) { 
    //Print to alert we entered method 
    print("task submitted") 
    //Get input from text field 
    let input = textField.text 
    //Add object to array, reload table 
    self.taskArray.addObject(input!) 
    self.taskTable.reloadData() 
}//addTask 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.taskTable.delegate = self 
    self.taskTable.dataSource = self 
    print("application finished loading") 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.taskArray.count; 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = self.taskTable.dequeueReusableCellWithIdentifier("cell")! 
    cell.textLabel?.text = self.taskArray.objectAtIndex(indexPath.row) as? String 
    return cell 
    } 
} 
+0

あなたの質問は何ですか? – Emptyless

+0

私が現在提示しているコードはうまくいきませんので、機能が得られるように変更が加えられたのかどうか疑問に思っています – jb990

答えて

0

ほとんどの場合、あなたの避難所を実際にViewControllerをUITableViewDelegateまたはUITableViewDataSourceにしました。あなたのストーリーボードで、あなたのUITableViewを右クリックし、デリゲートとデータソースが設定されていることを確認してください。そうでない場合は、それぞれの横の円からドラッグしてViewControllerに接続します。

+0

クリスおかげで、唯一の問題は以下のコメントスレッドにあります。 – jb990

0

は、次の行を追加します。

self.taskTable.delegate = self 
self.taskTable.dataSource = self 

編集:誤植

+0

興味深いことに、次のエラーが表示されます: "タイプ 'UITableView'の値に ' '" – jb990

+0

@ jb990資本金が –

+0

のGotchaのself.taskTable.dataSourceである必要があります。それらのエラーを解決しました。今すぐ問題が発生するのはランタイムエラーです: "let cell:UITableViewCell = self.taskTable.dequeueReusableCellWithIdentifier(" cell ")!"値が0であると主張していました – jb990

関連する問題