There are a number of ways to add rows to a table view at runtime. The built-in behavior for adding a row is to display a new row with a green plus sign icon. However, this technique has fallen out of favor in iOS applications because it’s cumbersome to enter editing mode and then find the row with the plus sign icon – especially in larger tables.
So we’re going to put a reloadData to the table view. That way, if you add a Possession to the PossessionStore, you can reload the table, and the new Possession will be included in the data sent to the table for display.
button in the header view instead. Recall that when a table view first appears on screen, it asks its data source for the data it needs to display, and the data source provides it. You can force this process to run again by sending the messageIn ItemsViewController.m, implement the action method for the New button so that a new random Possession is added to the store and the table is reloaded.
- (IBAction)addNewPossession:(id)sender { [[PossessionStore defaultStore] createPossession]; // tableView returns the controller's view [[self tableView] reloadData]; }
Notice the tableView message that the ItemsViewController sends itself. Every UITableViewController implements tableView, which returns the table controller’s view. Because we know this method returns an instance of UITableView, we can send it UITableView-specific messages, like reloadData.
Build and run the application and tap your
button. A new random possession will appear at the bottom of the table.Now that you have the ability to add rows, remove the code in the init method in ItemsViewController.m that immediately puts 10 random possessions into the store. The init method should now look like this:
- (id)init { // Call the superclass's designated initializer self = [super initWithStyle:UITableViewStyleGrouped]; return self; }
3.15.26.221