Adding Rows

There are two common interfaces for adding rows to a table view at runtime.

  • A button above the cells of the table view. This is usually for adding a record for which there is a detail view. For example, in the Contacts app, you tap a button when you meet a new person and want to take down all their information.

  • A cell with a green plus sign. This is usually for adding a new field to a record, such as when you want to add a birthday to a person’s record in the Contacts app. In edit mode, you tap the green plus sign next to add birthday.

In this exercise, you are using the New button in the header view instead. When this button is tapped, a new row will be added to the UITableView.

In BNRItemsViewController.m, implement addNewItem:.

-​ ​(​I​B​A​c​t​i​o​n​)​a​d​d​N​e​w​I​t​e​m​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​/​/​ ​M​a​k​e​ ​a​ ​n​e​w​ ​i​n​d​e​x​ ​p​a​t​h​ ​f​o​r​ ​t​h​e​ ​0​t​h​ ​s​e​c​t​i​o​n​,​ ​l​a​s​t​ ​r​o​w​
 ​ ​ ​ ​N​S​I​n​t​e​g​e​r​ ​l​a​s​t​R​o​w​ ​=​ ​[​s​e​l​f​.​t​a​b​l​e​V​i​e​w​ ​n​u​m​b​e​r​O​f​R​o​w​s​I​n​S​e​c​t​i​o​n​:​0​]​;​
 ​ ​ ​ ​N​S​I​n​d​e​x​P​a​t​h​ ​*​i​n​d​e​x​P​a​t​h​ ​=​ ​[​N​S​I​n​d​e​x​P​a​t​h​ ​i​n​d​e​x​P​a​t​h​F​o​r​R​o​w​:​l​a​s​t​R​o​w​ ​i​n​S​e​c​t​i​o​n​:​0​]​;​

 ​ ​ ​ ​/​/​ ​I​n​s​e​r​t​ ​t​h​i​s​ ​n​e​w​ ​r​o​w​ ​i​n​t​o​ ​t​h​e​ ​t​a​b​l​e​.​
 ​ ​ ​ ​[​s​e​l​f​.​t​a​b​l​e​V​i​e​w​ ​i​n​s​e​r​t​R​o​w​s​A​t​I​n​d​e​x​P​a​t​h​s​:​@​[​i​n​d​e​x​P​a​t​h​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​R​o​w​A​n​i​m​a​t​i​o​n​:​U​I​T​a​b​l​e​V​i​e​w​R​o​w​A​n​i​m​a​t​i​o​n​T​o​p​]​;​
}​

Build and run the application. Tap the New button and… the application crashes. The console tells you that the table view has an internal inconsistency exception.

Remember that, ultimately, it is the dataSource of the UITableView that determines the number of rows the table view should display. After inserting a new row, the table view has six rows (the original five plus the new one). Then, it runs back to its dataSource and asks it for the number of rows it should be displaying. BNRItemsViewController consults the store and returns that there should be five rows. The UITableView then says, Hey, that is not right! and throws an exception.

You must make sure that the UITableView and its dataSource agree on the number of rows. Thus, you must add a new BNRItem to the BNRItemStore before you insert the new row.

In BNRItemsViewController.m, update addNewItem:.

-​ ​(​I​B​A​c​t​i​o​n​)​a​d​d​N​e​w​I​t​e​m​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​N​S​I​n​t​e​g​e​r​ ​l​a​s​t​R​o​w​ ​=​ ​[​[​s​e​l​f​ ​t​a​b​l​e​V​i​e​w​]​ ​n​u​m​b​e​r​O​f​R​o​w​s​I​n​S​e​c​t​i​o​n​:​0​]​;​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​ ​n​e​w​ ​B​N​R​I​t​e​m​ ​a​n​d​ ​a​d​d​ ​i​t​ ​t​o​ ​t​h​e​ ​s​t​o​r​e​
 ​ ​ ​ ​B​N​R​I​t​e​m​ ​*​n​e​w​I​t​e​m​ ​=​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​c​r​e​a​t​e​I​t​e​m​]​;​

 ​ ​ ​ ​/​/​ ​F​i​g​u​r​e​ ​o​u​t​ ​w​h​e​r​e​ ​t​h​a​t​ ​i​t​e​m​ ​i​s​ ​i​n​ ​t​h​e​ ​a​r​r​a​y​
 ​ ​ ​ ​N​S​I​n​t​e​g​e​r​ ​l​a​s​t​R​o​w​ ​=​ ​[​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​a​l​l​I​t​e​m​s​]​ ​i​n​d​e​x​O​f​O​b​j​e​c​t​:​n​e​w​I​t​e​m​]​;​

 ​ ​ ​ ​N​S​I​n​d​e​x​P​a​t​h​ ​*​i​n​d​e​x​P​a​t​h​ ​=​ ​[​N​S​I​n​d​e​x​P​a​t​h​ ​i​n​d​e​x​P​a​t​h​F​o​r​R​o​w​:​l​a​s​t​R​o​w​ ​i​n​S​e​c​t​i​o​n​:​0​]​;​

 ​ ​ ​ ​/​/​ ​I​n​s​e​r​t​ ​t​h​i​s​ ​n​e​w​ ​r​o​w​ ​i​n​t​o​ ​t​h​e​ ​t​a​b​l​e​
 ​ ​ ​ ​[​s​e​l​f​.​t​a​b​l​e​V​i​e​w​ ​i​n​s​e​r​t​R​o​w​s​A​t​I​n​d​e​x​P​a​t​h​s​:​@​[​i​n​d​e​x​P​a​t​h​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​R​o​w​A​n​i​m​a​t​i​o​n​:​U​I​T​a​b​l​e​V​i​e​w​R​o​w​A​n​i​m​a​t​i​o​n​T​o​p​]​;​
}​

Build and run the application. Tap the New button and watch the new row slide into the bottom position of the table. Remember that the role of a view object is to present model objects to the user; updating views without updating the model objects is not very useful.

Also, notice that you are sending the message tableView to the BNRItemsViewController to get at the table view. This method is inherited from UITableViewController, and it returns the controller’s table view. While you can send the message view to an instance of UITableViewController and get a pointer to the same object, using tableView tells the compiler that the object returned will be an instance of class UITableView. Thus, sending a message that is specific to UITableView, like insertRowsAtIndexPaths:withRowAnimation:, will not generate a warning.

Now that you have the ability to add rows and items, remove the code in the init method in BNRItemsViewController.m that puts five random items into the store.

-​ ​(​i​n​s​t​a​n​c​e​t​y​p​e​)​i​n​i​t​
{​
 ​ ​ ​ ​/​/​ ​C​a​l​l​ ​t​h​e​ ​s​u​p​e​r​c​l​a​s​s​'​s​ ​d​e​s​i​g​n​a​t​e​d​ ​i​n​i​t​i​a​l​i​z​e​r​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​W​i​t​h​S​t​y​l​e​:​U​I​T​a​b​l​e​V​i​e​w​S​t​y​l​e​P​l​a​i​n​]​;​
 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​5​;​ ​i​+​+​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​c​r​e​a​t​e​I​t​e​m​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

Build and run the application. There will not be any rows when you first fire up the application, but you can add some by tapping the New button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset