2016-07-06 67 views
0

我的問題具體涉及以下一組操作:我希望用戶能夠將文本輸入到textField中,按下按鈕,然後讓該文本轉移到一個新的單元格中,然後新填充到表格中。按鈕添加單元格textField文本到表Xcode/Swift 2.0

我有一個視圖控制器與文本字段,按鈕和表對象。我還有一個數組,用於存儲每個字符串作爲用戶輸入文本,並且我的代碼如下所示:

更新1:添加「self.taskTable.delegate = self」和「self.taskTable.dataSource = self」正如評論中所建議的那樣。

更新2:忘了在故事板編輯器中添加標識符,代碼現在可用,謝謝大家!代碼如下內容:

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。在Storyboard中,右鍵單擊您的UITableView並確保設置了委託和數據源。如果不是,請從每個旁邊的圓圈拖出並將其連接到ViewController。

+0

感謝克里斯,唯一的問題是現在在下面的評論線程中的一個。 – jb990

0

你沒有在你的viewDidLoad()

添加代理引用添加以下內容:

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

編輯:錯字

+0

有趣的是,它提出了以下錯誤:「類型'UITableView'的值沒有成員'數據源'「 – jb990

+0

@ jb990它應該是self.taskTable.dataSource與大寫S –

+0

陷阱。清除這些錯誤。現在只有問題是運行時錯誤:「let cell:UITableViewCell = self.taskTable.dequeueReusableCellWithIdentifier(」cell「)!」聲稱值爲零被發現 – jb990

相關問題