My first View Controller has a TableView.
On a second view controller, the user will input data and click a button.
I want this button to add the data to the table view, but I can not figure out how to do this.
var textArray: NSMutableArray! = NSMutableArray()
@IBOutlet weak var tableView: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
@IBOutlet weak var addData: UIButton!
@IBAction func addData(sender: AnyObject) {
self.textArray.addObject(textField.text)
self.tableView.reloadData()
}
It also brings up an error for my reloadData
line of code.
Presumably the code you have quoted is in the second view controller. The IBAction in that view controller can't reload the self.tableview because that table is in the first view controller.
To do what you are looking to so there are literally dozens of approaches each with pluses and minuses. Maybe the easiest to implement is:
textArray
.self.textArray.addObject(textField.text)
.self.tableView.reloadData()
line to the viewWillAppear function of the first view controller.