IOS uitableview paging Loading

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/w59879213/article/details/7208288

I searched through the Internet and introduced many articles on uitableview paging. They are all very uniform and the code is correct. I just didn't sort out my ideas. I am here to give it a hand and tidy it up.

The premise here is that you have added uitableview to the view and implemented the uitableviewdelegate and uitableviewdatasource interfaces in the H file, it has been associated with the tblview defined in your background, and the datesource and delegete of uitableview have all pointed to the file's owner.

If you do not understand this, we recommend that you review how to use uitableview and study this part.

1. first, define the data source. uitableview requires a data source. I use the SQLite database, so I made a small paging query. the paging query of SQLite is the same as that of MySQL.

[SQL]View plaincopy

  1. Select * from table where column name = condition limit pages * Number of records per page, number of records per page

[CPP]View plaincopy

  1. + (Nsmutablearray *) getrecord :( nsinteger) P

  2. {// In the Code, except for the select operation of SQLite, the parameter P and the SQL syntax of the following pages are associated with paging.

  3. Nsstring * query = [nsstring stringwithformat: @ "select * from Table order by ID limit % d, 10", P-1) * 10];

  4. Char * select = (char *) [query utf8string];

  5. Nsmutablearray * array = [[nsmutablearray alloc] init];

  6. Sqlite3 * database;

  7. If (sqlite3_open ([dbobject getdatabasepath], & database) = sqlite_ OK)

  8. {

  9. Sqlite3_stmt * statement;

  10. If (sqlite3_prepare_v2 (Database, select,-1, & statement, nil) = sqlite_ OK)

  11. {

  12. While (sqlite3_step (statement) = sqlite_row)

  13. {

  14. // Use sqlite3_column_init, sqlite3_column_blob, and sqlite3_column_text to extract data from the record line. The code here is omitted.

  15. // Then assign the value to the nsdictionary array in the form of a key value.

  16. Nsdictionary * rowrecord = [[nsdictionary alloc] initwithobjectsandkeys: nsid, @ "ID", Data, @ "image ",

  17. Nsmessage, @ "message", nsvideourl, @ "videourl", nsaudiourl, @ "audiourl ",

  18. Nstoline, @ "toline", nssenddate, @ "senddate", nil];

  19. // Add nsdictionary to the nsmuablearray array.

  20. [Array addobject: rowrecord];

  21. }

  22. Sqlite3_finalize (statement );

  23. }

  24. Sqlite3_close (database );

  25. }

  26. Return array;

  27. }

Of course, this function must be declared in your h file before it can be implement in the M file. Otherwise, when you use the [class name Function Name: parameter] The method cannot be found during access.

 

2. now we have to go to our page for design. first, you must go to. declare the nsinteger currentpage variable in the H file. This variable is used to tell the system what page I am on.

[CPP]View plaincopy

  1. Nsinteger currentpage;

  2. @ Property (strong, nonatomic) iboutlet nsmutablearray * listdata;

  3. @ Property (strong, nonatomic) iboutlet uitableview * tbview;

3. during system initialization, set the variable value to 1 and assign values to the data source used by the uitableview object.

[CPP]View plaincopy

  1. -(Void) viewdidload

  2. {

  3. [Super viewdidload];

  4. // Do any additional setup after loading the view from Its nib.

  5. Currentpage = 1;

  6. Listdata = [dbmyclass getrecord: currentpage];

  7. }

4. what we need to do is to process the uitableview. Under normal circumstances, we return the total number of records in the numofrowsinsections, But we add the row paging button, so the total number of returned records must be added with 1

[CPP]View plaincopy

  1. -(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section

  2. {

  3. Nsinteger ROW = [listdata count];

  4. Return row + 1;

  5. }

5. then we can normally process our cell information in cellforrowatindexpath. For example, if we use a custom cell, we will load it here... this is the most important thing. You need to determine whether [indexpathrow] = and [listdatacount] have the same value. If they are the same, you need to load our paging button.[CPP]View
Plaincopy

  1. -(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath

  2. {

  3. Static nsstring * celltableidentifier = @ "cuscellfuturemessage ";

  4. Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: celltableidentifier];

  5. If (cell = nil)

  6. {

  7. If ([indexpath row] = [listdata count])

  8. {

  9. // Create a new cell and adjust its style to what we need.

  10. Cell = [[uitableviewcell alloc] initwithframe: cgrectzero

  11. Reuseidentifier: @ "loadmoreidentifier"];

  12. Cell. font = [uifont boldsystemfontofsize: 13];

  13. Cell. textlabel. Text = @ "Read more ...";

  14. }

  15. Else

  16. {

  17. // Initialization events of other cells. Read custom cells or set cells.

  18. }

  19. }

  20. Return cell;

  21. }

6. The event to be triggered after the uitableview is registered and clicked. The event must be defined in didselectrowatindexpath.[CPP]View plaincopy

  1. -(Void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath

  2. {

  3. If ([indexpath row] = [listdata count])

  4. {

  5. Uitableviewcell * loadmorecell = [tableview cellforrowatindexpath: indexpath];

  6. Loadmorecell. textlabel. Text = @ "reading more information... ";

  7. [Self defined mselectorinbackground: @ selector (loadmore) withobject: Nil];

  8. [Tableview deselectrowatindexpath: indexpath animated: Yes];

  9. Return;

  10. }

  11. Else

  12. {

  13. // Events of other cells

  14. }

  15. }

7. You may have noticed that when we set peformselectorinbackground for cells, we call the loadmore function. This function is the second core method here.

[CPP]View plaincopy

  1. -(Void) loadmore

  2. {// When you press this button, it means you need to check the next page. Therefore, the current page number is added to 1.

  3. Currentpage ++;

  4. Nsmutablearray * more = [dbmyclass getrecord: currentpage]; // retrieve data by calling the getrecord method.

  5. [Self defined mselecw.mainthread: @ selector (appendtablewith :) withobject: More waituntildone: No];

  6. }

  7. -(Void) appendtablewith :( nsmutablearray *) data

  8. {// Add nsmutablearray in loadmore to the original data source listdata.

  9. For (INT I = 0; I <[Data Count]; I ++ ){

  10. [Listdata addobject: [data objectatindex: I];

  11. }

  12. Nsmutablearray * insertindexpaths = [nsmutablearray arraywithcapacity: 10];

  13. For (INT ind = 0; ind <[Data Count]; ind ++ ){

  14. Nsindexpath * newpath = [nsindexpath indexpathforrow: [listdata indexofobject: [data objectatindex: Ind] insection: 0];

  15. [Insertindexpaths addobject: newpath];

  16. }

  17. // Call the uitableview method again to generate a line.

  18. [Self. tbview insertrowsatindexpaths: insertindexpaths withrowanimation: uitableviewrowanimationfade];

  19. }

These functions look a lot, but it is not complicated to take a closer look. After you implement the uitableview interface, apart from loadmore, appendtablewitdh, And the defined paging function getrecord, the rest are defined in the uitableview implementation interface.

Related Article

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.