01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Important protocols<br />

153<br />

executed. This is the manual instantiation of the cell, which is used only if there’s no<br />

cell to recycle that has recently been scrolled offscreen.<br />

This coding strategy is used in several other places in the iPhone SDK. To show that<br />

the view allocation is working, you make a project with a table view and implement<br />

dequeueReusableCellWithIdentifier just as you see right above. You fill in<br />

tableView: numberOfRowsInSection with the following:<br />

return 100;<br />

You can see the resulting application and its terminal output using the following code:<br />

Insert UITableViewDataSourceTerminalOutput.png and<br />

UITableViewDataSourceProtocolApp<br />

You have 100 cells that you can scroll through here, with 11 active on the screen at any<br />

one time. This means that at the minimum you must create 11 UITableViewCells.<br />

When you look at your terminal output, you can see that 12 UITableViewCells were<br />

created. This is efficient and ensures that as little memory as possible is used to allow<br />

the user to scroll through all 100 cells. This technique is used for memory management<br />

with the protocol as well. Whether you’re using a<br />

UITableViewCell standard class or a custom subclass, this should be your approach to<br />

deliver the cells to the view.<br />

OPTIONAL METHODS<br />

The following method is used when a table view is to have more than one section:<br />

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView<br />

When this method isn’t implemented, the method defaults to one. One thing to note<br />

is that the iPhone OS uses a zero-based count for section and row numbers, so the first<br />

element of the first section of a table view will have an index path like the following:<br />

NSIndexPath.row = 0<br />

NSIndexPath.section = 0<br />

The following code is used to set the title of the header for sections when using<br />

default table view headers with the UITableViewStyleDefault:<br />

- (NSString *)tableView:(UITableView *)tableView<br />

titleForHeaderInSection:(NSInteger)section<br />

Every section begins with a header and can also have a footer at the end. If the following<br />

method isn’t defined, there’ll be only headers, no footers, to separate your sections:<br />

- (NSString *)tableView:(UITableView *)tableView<br />

titleForFooterInSection:(NSInteger)section<br />

7.3.2 <br />

Unlike the , the has no required<br />

methods. All the methods in this protocol fall into the following functions:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!