ios - Need help adding data to a TableView -
my first view controller has tableview.
on second view controller, user input data , click button.
i want button add data table view, can not figure out how 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 brings error reloaddata
line of code.
presumably code have quoted in second view controller. ibaction in view controller can't reload self.tableview because table in first view controller.
to looking there literally dozens of approaches each pluses , minuses. maybe easiest implement is:
- creating public variable in second view controller called
textarray
. - set public variable in prepareforsegue data source of table in first view controller.
- add string variable in ibaction of second view controller
self.textarray.addobject(textfield.text)
. - add
self.tableview.reloaddata()
line viewwillappear function of first view controller.
Comments
Post a Comment