IPhoneOfUitableview
Code for pagination
Uitableview can display a lot of content in the list, which is also a common component in our development. We usually display the list by page, for example, display 10 first.
Click more to add 10 records, and so on. Below is a demo similar to more displays.
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