Home > 2010 Winter DPS913 > Introduction to Cocoa collection classes (array, dictionary)

Introduction to Cocoa collection classes (array, dictionary)

January 27, 2010 Leave a comment Go to comments

Cocoa has classes to hold collections of objects. We work with two today (array, dictionary) and later, we will work with a third (set).

The Apple documentation offers us two accessible introductions to Cocoa collection classes. I won’t bother rewording them here – instead, I’ll link you to them, and we’ll cover the concepts during the lecture.

Cocoa collections

From the Cocoa Core Competencies list of topics:


From the iPhone Dev Center reference documentation:

Collections Programming Topics for Cocoa

Go through the Arrays… and Dictionaries… topics.

Also, go through this topic: Fast Enumeration

Simple example app – Collections

The “Collections” example app gets us started with arrays and dictionaries. We’ll work through the code in class.

Simple example app – Picker

The “Picker” example app works with an array again, but with a twist this time. The array is a data source for a picker in the user interface.

How a  picker (UIPickerView) works

A picker needs a “delegate” and a “data source” to work properly. (Soon, you will use a table view, which also works in a similar way.

A picker is an object that is (in our situation) declared in a nib. We cannot write code in a nib, so we write code in the delegate and the data source. In our picker example, the one and only view controller will adopt the picker’s delegate and data source protocols.

The interesting thing about the picker (and the table view) is that it enables us to simply create a data source (typically an array), and the instantiation of the picker, when it loads during the view-loading phase, does most of the UI-building work on its own.

When the picker loads, it asks these questions:

1. How many components are in this picker view? (As you will learn, a picker can have one or more spinning dials; each one is called a “component”.)

For each component…

2. How many rows are in the component?

For each row…

3. What (text) should be displayed?

In effect, the picker loading runs the following pseudo-code (we do NOT write this code!):

for (int i = 0; i < numberOfComponents; i++) {
for (int i = 0; i < numberOfRowsInComponent; i++) {
rowDisplay = rowContent;

Our job is simply to answer these questions:

To answer question 1, we implement numberOfComponentsInPickerView:. Our BusLab2 needs only 1, so we return 1.

To answer question 2, we implement numberOfRowsInComponent:component:.

To answer question 3, we implement titleForRow:forComponent:.

Our methods will provide granular answers to the questions. The following code implements these three methods. “pickerBuilder” is my name for the picker’s array data source.




The other delegate method that we must implement is didSelectRow:inComponent:. This method runs whenever a new item is selected in the picker. The method is called from the user interface, passing the arguments for the component and row selected. Our method simply does “whatever” with the row and component values. In our case, we go back to the data source (always the data source!) and get some data. Here’s a sample implementation.




After implementing these methods, your picker should be able to display data.

Categories: 2010 Winter DPS913
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: