views:

56

answers:

2

Hi,

I've been tasked with creating an iPhone app which will tap into a series of REST API JSON feeds from Twitter.

I'm somewhat new to iPhone development, but I've created a few apps before, though none of them required tapping into API data.

So let's say we have 3 views in Interface Builder that are being controlled by a tabcontroller:

  • Feed
  • Followers
  • Following

Each of these views need to display a JSON feed in a tableview. The data should be loaded at the time of the view being shown.

There will also be a searchbox that makes a request to Twitter's search API JSON feed and returns results in a UITableView in another view.

I've installed and imported the json-interface library in my project. I have also successfully received the feed in an NSString.

But here's my questions:

  • How should I prep my application to make requests to multiple JSON feeds and display the respective data in different tables on different views? All the tutorials I see only show one JSON request being formatted into one table on one view. For instance: http://iphonedevelopertips.com/cocoa/json-framework-for-iphone-part-2.html

  • How does Interface Builder fit into this? How can I use the tables I inserted into my views in Interface Builder?

Any tutorial links or pertinent project source code would be much appreciated!

Thanks!

Dave

A: 

How should I prep my application to make requests to multiple JSON feeds and display the respective data in different tables on different views? ...

Each ViewController will take care of their own JSON requests like the tutorial.

How does Interface Builder fit into this? How can I use the tables I inserted into my views in Interface Builder?

You create a IBOutlet for your tableviews in your ViewControllers. For example:

// FeedViewController.h
@interface FeedViewController : UIViewControllerUIViewController <UITableViewDelegate, UITableViewDataSource, FeedControllerDelegate> {
IBOutlet UITableView _feedTableView
}

In Interface Builder you link your TableView to the IBOutlet variable. You'll use this variable to interact with the TableView from the controller.

Any tutorial links or pertinent project source code would be much appreciated!

When I started a similar project, I did not find a lot of good tutorials. For me, Tekpub was the best resource for figuring out how to display manipulate data in an iPhone app.

dr
@dr: Your link to Tekpub looks simply like an advertisement and you haven't even pointed out how it's helpful in this context. Maybe you think it's a great resource, but that doesn't mean that you should put this little "banner" on every answer on SO. I strongly suggest that you remove it.
Felixyz
@Felixyz: I see your point and adjusted my answer accordingly. I assume you will leave similar comments on answers that direct users to a book they can buy as well?
dr
@dr: There is nothing wrong with linking to a good resource or book, but I've personally never seen anything on SO that looked so much like an ad (except for the actual ads of course...) In my opinion, even your reformulated version is unhelpful. "I looked for free tutorials" — about what? How does this relate to the question? (Part of the problem is that the question itself is a bit vague.) I'm sure the resource is great and you want people to know about it, but SO is supposed to be about clear answers to clear questions.
Felixyz
+1, Felixyz ... looks like an ad to me!
CaliforniaDave
+1  A: 

What I'm suggesting below may or may not be a bit more than what your situation requires, but it's always a good idea to separate the data and networking from you user interface classes.

You should create a model layer, in other words a set of classes that are only responsible for getting data from the server and making it accessible to the rest of the app. The way to design a model layer is similar to designing a database: think about the data domain and what the most convenient way of mapping it is. (In this case your model classes will follow the twitter data model rather closely.) It is also important not to think too much about what the interface will look like. The model layer should be as independent of the UI as possible, so that changing the UI will not require changes to the model classes.

Create a base model object that you can then subclass to represent specific kinds of data. This base model should know how to make a call to the server, and it should have a state showing if the data is loading, completed, or if the loading has been canceled. UI classes such as views and controllers can observe this state and update themselves when it changes.

For example, each table view cell might have a reference to a model object, and when the model's state changes to "loaded", the cell will get some strings and other data from the model and update its appearance.

Take care not to have too many network requests going at the same time. Instead, use a queue. You should definitely use the fantastic ASIHTTPRequest library for networking and queuing. To work with a model layer as outlined, you also need to understand Key-Value Observing.

As for your second question, it's very general. You use the tables by making your controller the dataSource and delegate and implementing the UITableViewDelegate and UITableViewDataSource protocols.

Felixyz
Thanks for the well thought out answer!
CaliforniaDave