Lab 7 - Table Views
Objectives
- Use table views
- Use a property list
- Fetch data using web services
You will extend your lab6 project by reading a list of twitter users from a
property list. We will fetch the data from Twitter. You will update the
PersonListViewController to manage the data with a table view and update the
Detail view by having a list of status entries, also in a table view.
- You will want to inspect the contents of the property list in a text
editor.
To read the TwitterUsers.plist file you will want to use the
NSBundle class to get the path for the property list. This can be done
with [[NSBundle mainBundle] resourcePath] which will give you an NSString *
containing the file path to your resources. You can use this to create
a complete file path to your TwitterUsers.plist file so that you can use +[NSArray arrayWithContentsOfFile:]
to get the contents of your plist (an array of your twitter users).
This all will be done in your table view controller so hold tight for now.
- We need a model object for a person. Create a Person class which
should have an image (or most likely an image URL), the username for Twitter, a display
name, and a list of status updates.
- You have been supplied with a class TwitterHelper with methods to
fetch information about the given username and fetch an array of status
updates. Drag the header and source files into your Xcode project as
well as the JSON folder. You don't need to know how these work to use
them. The +fetchInfoForUsername: method returns a dictionary.
This dictionary has keys: "screen_name", "name", "profile_image_url", and
"status".
- For the table view with the PersonListViewController, you will want to
use UITableViewController as the starting point for your view controller
subclass. You can do this by adding an ObjectiveC class with the
subclass of UITableViewController. This automatically creates a table view with itself as the
delegate and datasource. You don't need a nib if all you are
displaying is the table. Instantiate this class using -initWithStyle:.
- You will need to implement -tableView: numberOfRowsInSection: , -tableView:
cellForRowAtIndexPath:, -tableView: didSelectRowAtIndiexPath:, and you
probably want to add UITableView delegate method tableView:
accessoryTypeForRowWithIndexPath:
- You now want to handle the detail views in response to a table
selection. The PersonDetailViewController class will also be a table
view controller and should have a "person"
property which will be set before you push the view on the stack.
- To display the status update which will vary in size, you will use the
UITableView delegate method for specifying the height of each row. To
compute the height of your text, use the methods for UIStringDrawing.h.
In a table view with the grouped style, the cell is inset by 10 pixels from
the edges, and the text is inset by an additional 10 pixels.
The status value is a dictionary value with key "text". Get this text
value and the font [UIFont systemFontOfSize: 12]. If we constrain the
size to CGSizeMake(100,1000) in the UIStringDrawing method -sizeWithFont:
constrainedToSize: lineBreakMode: we should get the size. Calculate
the appropriate height from that value.
Be sure to customize the contentView of your table view cell by adding your own
subview since UITableViewCell does not handle multiline text by default.
In tableView: cellForRowAtIndexPath: you must set the
cell.textLabel.numberOfLines to 0. Then you can set the
cell.textLabel.lineBreakMode and cell.textLabel.text appropriately for your
multiline status values.
- Compress your project and send it to me through the Dropbox in
BlackBoard.