我的问题具体涉及以下一组操作:我希望用户能够将文本输入到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