O'Reilly Books Example updated: Show a different image for each book

By Elisabeth Robson
July 12, 2009 | Comments: 5

I got some great feedback on my first screencast - thank you! A few of you asked: how can you show a different view for each of the rows in the table view, instead of just showing the same view for each row?

I can think of a couple of ways to do this. One is to create different view for each row by creating a separate class for each type of view you want to display, and then select the specific view to display in tableview:didSelectRowAtIndexPath: in the BooksTableViewController depending on which row is selected.

As another option, I decided to use the same view, the BookDetailView, but customize the view with a different image for each book. To do this, I created a new model class, BooksModel, and stored the titles of the books, and images for each book in this class. Then, based on the row selected, I set the appropriate image in the view (from tableview:didSelectRowAtIndexPath: when the view controller for the detail view is created) before displaying the view.

Here's a new screencast that expands on the previous O'Reilly Books example. I have also attached the updated code below. I hope this answers your questions, and if you have any feedback, please do leave a comment.

Or, to follow along more closely with the code, Click here to view or download the original high resolution .mov file. [155 Mb]

Update: Here's the downloadable code sample



You might also be interested in:


5 Comments

hi
thanks for this helpful example .

i am asking if i want to view 2 or more images each view that you made gust one image in each view. and to move from one to another by touching and moving ?

thanks agin

This has been a nice pair of tutorials-- thanks much!

A question related to the change from "cell.text" to "cell.textLabel":
Your example has this code:
UILabel *cellLabel = cell.textLabel;
cellLabel.text = [self.booksModel getBookTitleAtIndex:row];

Can one not set the text directly:
cell.textLabel = [self.booksModel getBookTitleAtIndex:row];

Why do you create the intermediate object?

Thanks for any info!
Peter

Peter -
That's a good question - I don't think I needed to create the intermediate object at all. At the time, I probably did it because the cell label was new to me, so I was being really explicit in my thinking about it.

Thanks for the comment!

Elisabeth

Hi Elisabeth!

Loved both tutorials. Really helpful for me in getting past a stubborn sticking point. :) And, you've got a GREAT voice and pace for screencasts. Please do more! Regarding the deprecated text property for cell, I believe the correct syntax (without the need of an intermediate syntax) is:

cell.textLabel.text = [self.booksModel getBookTitleAtIndex:row];

Just using cell.textLabel throws an error for me.

Best ~ Scott

Hi Elizabeth, As a complete novice I've worked though both your screencasts, and found them extremely helpful. One question on the above, though concerns the alternative way of showing a different view depending on which row is selected. You mention:

"One is to create different view for each row by creating a separate class for each type of view you want to display, and then select the specific view to display in tableview:didSelectRowAtIndexPath: in the BooksTableViewController depending on which row is selected."

Can you show me how this might be implemented. I'd list to add different text views rather than picture views.

Thanks for the great tutorials,

kev.

Leave a comment



Popular Topics

Browse Books

News Topics


Got a Question?