IPhoneUsed inUITableViewImplementationPagingThe effect is what we will introduce in this article,UITableviewThe ability to list and display a lot of content is also a frequently used component in our development. We oftenPagingDisplay the list, such as displaying 10 records first, clicking more to add 10 records, and so on. Below is a demo similar to displaying more records.
The implementation result is as follows:
Click "More ...", Achieve the following results.
Implementation ideas:
Basically, there are only 10 entries in the data source. When you click the last cell, add more data to the data source.
Process the selection event of the cell "load more" and trigger a method to load more data to the list.
IndexPathForRow inserts data.
The implementation process is as follows:
- # Import <UIKit/UIKit. h>
-
- @ Interface iphone_tableMoreViewController: UIViewController
- <UITableViewDelegate, UITableViewDataSource> {
- IBOutlet UITableView * myTableView;
- NSMutableArray * items;
- }
- @ Property (nonatomic, retain) UITableView * myTableView;
- @ Property (nonatomic, retain) NSMutableArray * items;
- @ End
-
- # Import "iphone_tableMoreViewController.h"
- @ Implementation iphone_tableMoreViewController
- @ Synthesize items, myTableView;
- -(Void) viewDidLoad {
- [Super viewDidLoad];
- Items = [[NSMutableArray alloc] initWithCapacity: 0];
- For (int I = 0; I <10; I ++ ){
- [Items addObject: [NSString stringWithFormat: @ "cell % I", I];
- }
- }
- -(Void) didReceiveMemoryWarning {
- [Super didReceiveMemoryWarning];
- }
-
- -(Void) viewDidUnload {
- Items = nil;
- Self. myTableView = nil;
- }
- -(Void) dealloc {
- [Self. myTableView release];
- [Items release];
- [Super dealloc];
- }
-
- -(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
- Int count = [items count];
- Return count + 1;
- }
- -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
- Static NSString * tag = @ "tag ";
- UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: tag];
- If (cell = nil ){
- Cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero
- ReuseIdentifier: tag] autorelease];
- }
- If ([indexPath row] = ([items count]) {
- // Create loadMoreCell
- Cell. textLabel. text = @ "More ..";
- } Else {
- Cell. textLabel. text = [items objectAtIndex: [indexPath row];
- }
- Return cell;
- }
- -(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
-
- If (indexPath. row = [items count]) {
- UITableViewCell * loadMoreCell = [tableView cellForRowAtIndexPath: indexPath];
- LoadMoreCell. textLabel. text = @ "loading more... ";
- [Self defined mselectorinbackground: @ selector (loadMore) withObject: nil];
- [TableView deselectRowAtIndexPath: indexPath animated: YES];
- Return;
- }
- // Other cell events
- }
- -(Void) loadMore
- {
- NSMutableArray * more;
- More = [[NSMutableArray alloc] initWithCapacity: 0];
- For (int I = 0; I <10; I ++ ){
- [More addObject: [NSString stringWithFormat: @ "cell ++ % I", I];
- }
- // Load your data
- [Self defined mselecw.mainthread: @ selector (appendTableWith :) withObject: more waitUntilDone: NO];
- [More release];
- }
- -(Void) appendTableWith :( NSMutableArray *) data
- {
- For (int I = 0; I <[data count]; I ++ ){
- [Items addObject: [data objectAtIndex: I];
- }
- NSMutableArray * insertIndexPaths = [NSMutableArray arrayWithCapacity: 10];
- For (int ind = 0; ind <[data count]; ind ++ ){
- NSIndexPath * newPath = [NSIndexPath indexPathForRow: [items indexOfObject: [data objectAtIndex: ind] inSection: 0];
- [InsertIndexPaths addObject: newPath];
- }
- [Self. myTableView insertRowsAtIndexPaths: insertIndexPaths withRowAnimation: UITableViewRowAnimationFade];
- }
- @ End
Source code: http://easymorse-iphone.googlecode.com/svn/trunk/iphone.tableMore/
Summary:IPhoneUsed inUITableViewImplementationPagingResults With code) content is introduced, hope to learn through this article to help you!