UITableViewController

A UITableView is a view object. Recall in the Model-View-Controller design pattern, which iOS developers do their best to follow, each class is exactly one of the following:

  • Model: Holds data and knows nothing about the user interface.

  • View: Is visible to the user and knows nothing about the model objects.

  • Controller: Keeps the user interface and the model objects in sync. Controls the flow of the application; for example, the controller might be responsible for showing a Really delete this item? message before actually deleting some data.

Thus, a UITableView, a view object, does not handle application logic or data. When using a UITableView, you must consider what else is necessary to get the table working in your application:

  • A UITableView typically needs a view controller to handle its appearance on the screen.

  • A UITableView needs a data source. A UITableView asks its data source for the number of rows to display, the data to be shown in those rows, and other tidbits that make a UITableView a useful user interface. Without a data source, a table view is just an empty container. The dataSource for a UITableView can be any type of Objective-C object as long as it conforms to the UITableViewDataSource protocol.

  • A UITableView typically needs a delegate that can inform other objects of events involving the UITableView. The delegate can be any object as long as it conforms to the UITableViewDelegate protocol.

An instance of the class UITableViewController can fill all three roles: view controller, data source, and delegate.

UITableViewController is a subclass of UIViewController, so a UITableViewController has a view. A UITableViewController’s view is always an instance of UITableView, and the UITableViewController handles the preparation and presentation of the UITableView.

When a UITableViewController creates its view, the dataSource and delegate instance variables of the UITableView are automatically set to point at the UITableViewController (Figure 8.4).

Figure 8.4  UITableViewController-UITableView relationship

UITableViewController-UITableView relationship

Subclassing UITableViewController

Now you are going to write a subclass of UITableViewController for Homepwner. For this view controller, you will use the NSObject template. From the File menu, select New and then File.... From the iOS section, select Cocoa Touch, choose Objective-C class, and click Next. Then, select NSObject from the pop-up menu and enter BNRItemsViewController as the name of the new class. Click Next and then click Create on the next sheet to save your class.

Open BNRItemsViewController.h and change its superclass:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>@​i​n​t​e​r​f​a​c​e​ ​B​N​R​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​N​S​O​b​j​e​c​t
#​i​m​p​o​r​t​ ​<​U​I​K​i​t​/​U​I​K​i​t​.​h​>​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​T​a​b​l​e​V​i​e​w​C​o​n​t​r​o​l​l​e​r​

The designated initializer of UITableViewController is initWithStyle:, which takes a constant that determines the style of the table view. There are two options: UITableViewStylePlain and UITableViewStyleGrouped. These looked quite different on iOS 6, but the differences are quite minor as of iOS 7.

You are changing the designated initializer to init. As such, you need to follow the two rules of initializers:

  • Call the superclass’s designated initializer from yours

  • Override the superclass’s designated initializer to call yours

Do both in BNRItemsViewController.m:

#​i​m​p​o​r​t​ ​"​B​N​R​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​

-​ ​(​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​]​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

-​ ​(​i​n​s​t​a​n​c​e​t​y​p​e​)​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​)​s​t​y​l​e​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​[​s​e​l​f​ ​i​n​i​t​]​;​
}​

This will ensure that all instances of BNRItemsViewController use the UITableViewStylePlain style, no matter what initialization message is sent to them.

Open BNRAppDelegate.m. In application:didFinishLaunchingWithOptions:, create an instance of BNRItemsViewController and set it as the rootViewController of the window. Make sure to import the header file for BNRItemsViewController at the top of this file.

#​i​m​p​o​r​t​ ​"​B​N​R​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​A​p​p​D​e​l​e​g​a​t​e​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​ ​=​ ​[​[​U​I​W​i​n​d​o​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​[​[​U​I​S​c​r​e​e​n​ ​m​a​i​n​S​c​r​e​e​n​]​ ​b​o​u​n​d​s​]​]​;​
 ​ ​ ​ ​/​/​ ​O​v​e​r​r​i​d​e​ ​p​o​i​n​t​ ​f​o​r​ ​c​u​s​t​o​m​i​z​a​t​i​o​n​ ​a​f​t​e​r​ ​a​p​p​l​i​c​a​t​i​o​n​ ​l​a​u​n​c​h​

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

 ​ ​ ​ ​/​/​ ​P​l​a​c​e​ ​B​N​R​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​'​s​ ​t​a​b​l​e​ ​v​i​e​w​ ​i​n​ ​t​h​e​ ​w​i​n​d​o​w​ ​h​i​e​r​a​r​c​h​y​
 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​r​o​o​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​=​ ​i​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​;​

 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​a​c​k​g​r​o​u​n​d​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​w​h​i​t​e​C​o​l​o​r​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​w​i​n​d​o​w​ ​m​a​k​e​K​e​y​A​n​d​V​i​s​i​b​l​e​]​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

Build and run your application. You should see an empty screen, as shown in Figure 8.5 – but there is an empty table view there. As a subclass of UIViewController, a UITableViewController inherits the view method. This method calls loadView, which creates and loads an empty view object if none exists. A UITableViewController’s view is always an instance of UITableView, so sending view to the UITableViewController gets you a bright, shiny, and empty table view.

Figure 8.5  Empty UITableView

Empty UITableView

Your table view needs some rows to display. Remember the BNRItem class you wrote in Chapter 2? Now you are going to use that class again: each row of the table view will display an instance of BNRItem.

Locate the header and implementation files for BNRItem (BNRItem.h and BNRItem.m) in Finder and drag them onto Homepwner’s project navigator.

When dragging these files onto your project window, select the checkbox labeled Copy items into destination group’s folder when prompted. This will copy the files from their current directory to your project’s directory on the filesystem and add them to your project.

You will not need the container or containedItem properties ever again (they were just to demonstrate strong reference cycles), so delete them from BNRItem.h:

            
              @​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​s​t​r​o​n​g​)​ ​B​N​R​I​t​e​m​ ​*​c​o​n​t​a​i​n​e​d​I​t​e​m​;​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​w​e​a​k​)​ ​B​N​R​I​t​e​m​ ​*​c​o​n​t​a​i​n​e​r​;​
            
          

Also delete the setContainedItem: method in BNRItem.m:

-​ ​(​v​o​i​d​)​s​e​t​C​o​n​t​a​i​n​e​d​I​t​e​m​:​(​B​N​R​I​t​e​m​ ​*​)​i​
{​
 ​ ​ ​ ​_​c​o​n​t​a​i​n​e​d​I​t​e​m​ ​=​ ​i​;​
 ​ ​ ​ ​/​/​ ​W​h​e​n​ ​g​i​v​e​n​ ​a​n​ ​i​t​e​m​ ​t​o​ ​c​o​n​t​a​i​n​,​ ​t​h​e​ ​c​o​n​t​a​i​n​e​d​
 ​ ​ ​ ​/​/​ ​i​t​e​m​ ​w​i​l​l​ ​b​e​ ​g​i​v​e​n​ ​a​ ​p​o​i​n​t​e​r​ ​t​o​ ​i​t​s​ ​c​o​n​t​a​i​n​e​r​
 ​ ​ ​ ​s​e​l​f​.​c​o​n​t​a​i​n​e​d​I​t​e​m​.​c​o​n​t​a​i​n​e​r​ ​=​ ​s​e​l​f​;​
}​

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

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