The tableView Workshop Series: Selecting a Row

The main goal of this workshop is to show you how to implement the row selection event performed by the app user.

What We’ll Do?

When the user is presented with a list of categories in the MasterViewController’s tableView control, he should be able to tap a row. The app should respond by displaying a second view that list item names belonging to the selected category, in a tableView control; thus satisfying this workshop’s goal.
workshopseries-figh15
Here’s what we’ll do:

Add Code in The AppDelegate Class

We will add code in the AppDelegate class to declare and implement a method called fetchShoppingListItems in the AppDelegate class. It will fetch and return all content from the shoppingListItems.plist file.

Create and Populate a Second Plist File

We will use the createPlistFile method to create a second plist file called shoppingListItems in the application sandbox’s Documents directory. We will use Xcode’s property list editor to add some categories and items in it. We could set up a view and class to permit the app user to add data in the plist file, but we won’t.

Add Code in The MasterViewController Class

Once the plist file is created and populated, we will add code in several methods of the MasterViewController class. One of them is the didSelectRowAtIndexPath method. Code you’ll add in this method will basically pass the category the user selected from the view’s tableView to the DetailViewController class, then load that class’ view in the Simulator.

Add Code in The DetailViewController Class

We’ll have to add code in the DetailViewController class to fetch item names belonging to the category, passed from the MasterViewController class, then list them in the DetailViewController’s tableView control.

Add Code in The AppDelegate Class

The first thing I want you to do is declare and implement the fetchShoppingListItems method in the AppDelegate.h file. As you can see, the method’s code is very much like the fetchShoppingListCategories method’s code.

Create and Populate a Second Plist File

Add this statement in the AppDelegate’s didFinishLaunchingWithOptions method, which create a second plist file in the app’s Documents directory.

After enter above code in the didFinishLaunchingWithOptions method, run the application so it can create the shoppingListItems.plist file. Use Finder to verify that the shoppingListItmes.plist file was created in the application sandbox’s Documents folder.

Double-click the shoppingListItems.plist file to open it in the property list editor and add these items in it don’t forget to save the file before closing it.
shopplistItems-plist-filecontent

Add Code in The MasterViewController Class

Once the plist file is created and populated, return to Xcode add this code in the MasterViewController.h file.

Switch to the MasterViewController.m file and add highlighted code.

Next, add this code in the viewDidLoad method

Here’s the code to add in the cellForRowAtIndexPath method.

Here’s the code to add in the MasterViewController.m file’s didSelectRowAtIndexPath method.

After enter code in the didSelectRowAtIndexPath method, run the application. The MasterViewController’s view will look like this:

workshopseries-fig13

Go ahead and tap any row, the detailViewController’s view is load in the device’s window. As you can see, the category name you selected is displayed in the view’s navigation bar.

What you’ve done up to this point is implemented code in the AppDelegate class and the MasterViewController class to fulfill the workshop’s goal, which is to implement the row selection event performed by the app user. All that’s left is to add code in the DetailViewController class to fetch item names belonging to the category, passed to the class, then list them in the view’s tableView control.

Add Code in The DetailViewController Class

I want you to drag a tableView control from the Object Library and drop it on the view’s canvas. Make sure it covers the view’s surface. Next, connect the control’s dataSource and delegate to the File’s Owner object in Interface Builder, the add this code in the DetailViewController.h file.

Switch to the DetailViewController.m file and add this statement.

Next, add this code code in the file.

Here is the final set of code to add in the file.

That’s all the code you need to add in the DetailViewController.m file, so run the application, tap a row; for example, Health & Beauty. The app respond by displaying items belonging to the category you selected in the DetailViewController’s tableView control like this:
workshopseries-figh14