我的問題具體涉及以下一組操作:我希望用戶能夠將文本輸入到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
}
}
什麼是你的問題? – Emptyless
以及我目前介紹的代碼不起作用,所以我想知道是否有任何更改,我可以使其功能 – jb990