Code for implementing paging display Using IOS-uitableview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.