I bet this question has been asked a thousand times, but I wasn't able to find one that directly relates to the problem I'm having right now, so I'd really appreciate some help from you guys. Here goes:
I have a view controller in my iPhone application that, when triggered, requests a JSON string from a server. When the app receives the JSON it gets parsed, and a table view is populated with the corresponding data. While this is taking place, I'm showing a UIActivityIndicatorView
to indicate that something is going on behind the scenes.
I can download and parse the JSON just fine, but when it comes to populating the table view, I'm pretty lost. Since I have to display a completely white view and not an empty table view with an activity indicator on it, I haven't made my view controller a subclass of UITableViewController (which by default displays an empty list).
How do I create and populate a table view with the data that I've downloaded from the server, and how 开发者_JS百科do I do it without the normal cellForRowAtIndexPath
and those kinds of methods?
Thanks!
Solution
I just figured out how to solve my problem. When the server response has returned, I allocate my table view, set its datasource and delegate to self
, then populate the table view with the returned data by implementing the same methods that a UITableViewController implements. Remember, as @Deepak points out, to adopt the UITableViewDatasource
and UITableViewDelegate
in the header file.
I don't see why you can't use a UITableViewController
subclass and do
// Create UIActivityIndicatorView instance
[self.view addSubview:activityIndicatorView];
If you need an empty white view, you can also add it as a subview here.
As such if you are interested in continuing in the current way. You will need to have your UIViewController
subclass adopt the UITableViewDatasource
and UITableViewDelegate
protocols and implement the appropriate methods. I don't think you can use a UITableView
without doing that.
Edit
It is very strange that when you take out the NIB, this behaves differently. If you change the number of rows to even 1
, the overlay shows correctly. This seems to be a bug.
I suggest that you implement loadView
to fix this where you separate self.view
and self.tableView
.
- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
精彩评论