views:

439

answers:

3

I am loading approximately 60 to 80 data from web-service.

But If you have seen "Around Me - Google - Application" -> It has a tableView as a search result -> In table View last cell is written as view More -> On clicking that more data appears

I want to implement the same effect / logic to my application but don't know how?

Would you plz help me?

+1  A: 

First you need to do paging in your application. That means, that instead of your web service returning all the items, it should return only some of them by adding a parameter of offset, meaning where to start getting results, and length, meaning how many results to get. This means the first call would be with 0, 20, the second with 20, 20, the third with 40, 20 etc if you want 20 items per "page".

Then, you add a clickable item to your table view. This can be a special section returned by your delegate, containing a single row which, when clicked, calls the web service for the next page and adds the result to the table view by updating the back-end array and using the insert row messages.

Aviad Ben Dov
+1  A: 

You can always customize your tableview. For example, lets say you wana to show only 25 items. Then in numberOfRowsInSection function return 26 and and in cellForRowAtrIndexPath, check for the last row. If it is the last row, then its text will be "View More".

Similarly in selectedRowAtIndexPath you can check for row count and if it is the last load another 25. Keep a integer index which will be 1 for first 25 and 2 for next 25. This way you dont have to retrive the data again and again, whereas you can store the data in a NSMutableArray.

rkb
+2  A: 

Aviad Ben Dov and rkbang offer some useful advice. Adding to that:

To respond to a touch on your "View More" cell, write a tableView:willSelectRowAtIndexPath: method (from the UITableViewDelegate protocol) in whichever object is the delegate for your UITableView. In this method, make a call to load more data, and return nil so the row does not stay selected (which is more button-like behavior):

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self loadMoreData];
    return nil;  // Don't leave this row selected.
}

In your load function, make a server call to get the data, parse it, and reload the table. It's also polite to give a busy indicator while you're waiting for the server reply:

#import "HTTPHelper.h"

// ... later ...

- (void) loadMoreData {
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  NSString* dataStr = nil;
  NSError* error = [[HTTPHelper sharedInstance]
                    synchronousGetURLAsString:@"http://server.com/mystuff"
                    replyData:&dataStr];
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  if (error) {
    [self displayErrorMessage:error];
  } else {
    [self parseIntoDataSource:dataStr];
    [tableView reloadData];
  }
}

In this last function, I'm assuming the method parseIntoDataSource will parse the server reply and add that data to whatever your class uses to provide the data for table cells which are given by the tableView:cellForRowAtIndexPath: method (of the UITableViewDataSource protocol).

That code also uses HTTPHelper from the moriarty library.

If you want to parse XML, you can use the SDK's own NSXMLParser. The TouchXML library is also available, and offers a little more robust support for slightly dirty data (like real-world html, which does not usually conform to XML standards). To parse JSON, you could use the json-framework, which has a very simple category-based interface.

Tyler
Is there a good reason to use tableView:willSelectRowAtIndexPath: vs. tableView:didSelectRowAtIndexPath:?
Jeff Kelley