views:

124

answers:

2

I've got a UITableView and I want to have a 'next 25' feature like in the app store app.

However, I've no idea what control they use and how to develop it.

Can someone shed light on it and give me as much info a possible ?

+3  A: 

What they do is create another cell, with a button in it. When that button is tapped, some method is fired which goes out and fetches more data, and then adds it to the tableview's data source, and reloads the table.

This "load more" cell never relies on the data source (it gets added without asking the DS for anything, remember that), instead, special case handling code in tableView:cellForRowAtIndexPath: is added to check if we're at the bottom of the tableview, and then return the custom cell. So when we reload the tableview after adding the new items to the data source, the tableview gets bigger, and the button goes away from that position, and gets added to the bottom when we get to the new bottom. :)

jer
OK great, so once the custom cell button is clicked, how do I refresh / redraw the table with the extra rows ?
Jules
I told you. In the button handler, you initiate a connection to a server to pull in the new data that is to be added to your data source. Once you get the data, and process it (this involves adding it to the table view data source), you call `reloadData` which reloads the table view, and magically, your new items will show up.
jer
A: 

The first half of the answer above is correct. The second half (using reloadData) isn't the best way to do it.

Once you have your new data, you should call insertRowsAtIndexPaths:withRowAnimation: on your table view. That tells the table view that you've added a bunch of new rows and lets it animate the addition of the new rows. For example, you might pass UITableViewRowAnimationFade or UITableViewRowAnimationTop. It's also more efficient, because the table view doesn't have to reload and redraw the rows that are already in the table.

Also, take a look at some of the videos on table views from the 2010 WWDC (you can find them at http://developer.apple.com/iphone and then scroll to the bottom). They've got lots of great stuff.

Jacques
You know, sometimes I leave things up as an exercise for the reader. It helps them learn how to do things on their own, which actually helps them remember things much better than continually having answers spoon fed to them. Explain an algorithm, hint at what they need to do to implement it, let them implement it and figure out their own techniques for how they want it to appear. Ask those questions if they need to.
jer
jer: Pointing users in the wrong direction is not helpful. `reloadData` breaks edit/reorder/selection states, breaks `UIView` animations on the table view, and makes for a poor user experience in general.
rpetrich
And if you read his question, he's not talking about editing/reordering. He's actually talking about pagination, which strongly suggests he's not looking to do any of that. It's not leading him in the wrong direction, and while it is not the right solution for every case, animating an insert like this answer suggests is almost certainly going to look like ass (i tested it in one of my apps that does this same thing). In addition, tell Apple it's a bad user experience. The App Store app does exactly what my answer provided, why? Because that's the better user experience for pagination cases.
jer
jer: I wasn't suggesting that inserting cells should be animated, only that `reloadData` should not __ever__ be called while the table view is visible. The `UITableViewRowAnimationNone` option exists for a reason--it is exactly what the App Store application uses. The poor user experience I am referring to is all the little user interaction details/animations that `reloadData` breaks, not the lack of animation (I agree, animation would "look like ass" in this case)
rpetrich
OK so how do I load data with insertRowsAtIndexPaths without using the data source? At the moment I pass an array into the data source.[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:[rs stringForColumn:@"desc"],@"desc", [rs stringForColumn:@"due"],@"due", [rs stringForColumn:@"price"],@"price", [rs stringForColumn:@"accumulated_price"],@"accum",nil]]; self.mydatasource = arratTmp
Jules
Jules: update the datasource before calling `insertIndexPaths:withRowAnimation:`. The indexPaths parameter should contain an `NSIndexPath` specifying the location of each row you've added to the data source
rpetrich
(or an `NSArray` of `NSIndexPaths` rather)
rpetrich