Use UITableView in iPhone to implement paging effect (with code)

Source: Internet
Author: User

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:

 
 
  1. # Import <UIKit/UIKit. h>
  2.  
  3. @ Interface iphone_tableMoreViewController: UIViewController
  4. <UITableViewDelegate, UITableViewDataSource> {
  5. IBOutlet UITableView * myTableView;
  6. NSMutableArray * items;
  7. }
  8. @ Property (nonatomic, retain) UITableView * myTableView;
  9. @ Property (nonatomic, retain) NSMutableArray * items;
  10. @ End
  11.  
  12. # Import "iphone_tableMoreViewController.h"
  13. @ Implementation iphone_tableMoreViewController
  14. @ Synthesize items, myTableView;
  15. -(Void) viewDidLoad {
  16. [Super viewDidLoad];
  17. Items = [[NSMutableArray alloc] initWithCapacity: 0];
  18. For (int I = 0; I <10; I ++ ){
  19. [Items addObject: [NSString stringWithFormat: @ "cell % I", I];
  20. }
  21. }
  22. -(Void) didReceiveMemoryWarning {
  23. [Super didReceiveMemoryWarning];
  24. }
  25.  
  26. -(Void) viewDidUnload {
  27. Items = nil;
  28. Self. myTableView = nil;
  29. }
  30. -(Void) dealloc {
  31. [Self. myTableView release];
  32. [Items release];
  33. [Super dealloc];
  34. }
  35.  
  36. -(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
  37. Int count = [items count];
  38. Return count + 1;
  39. }
  40. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
  41. Static NSString * tag = @ "tag ";
  42. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: tag];
  43. If (cell = nil ){
  44. Cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero
  45. ReuseIdentifier: tag] autorelease];
  46. }
  47. If ([indexPath row] = ([items count]) {
  48. // Create loadMoreCell
  49. Cell. textLabel. text = @ "More ..";
  50. } Else {
  51. Cell. textLabel. text = [items objectAtIndex: [indexPath row];
  52. }
  53. Return cell;
  54. }
  55. -(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
  56.  
  57. If (indexPath. row = [items count]) {
  58. UITableViewCell * loadMoreCell = [tableView cellForRowAtIndexPath: indexPath];
  59. LoadMoreCell. textLabel. text = @ "loading more... ";
  60. [Self defined mselectorinbackground: @ selector (loadMore) withObject: nil];
  61. [TableView deselectRowAtIndexPath: indexPath animated: YES];
  62. Return;
  63. }
  64. // Other cell events
  65. }
  66. -(Void) loadMore
  67. {
  68. NSMutableArray * more;
  69. More = [[NSMutableArray alloc] initWithCapacity: 0];
  70. For (int I = 0; I <10; I ++ ){
  71. [More addObject: [NSString stringWithFormat: @ "cell ++ % I", I];
  72. }
  73. // Load your data
  74. [Self defined mselecw.mainthread: @ selector (appendTableWith :) withObject: more waitUntilDone: NO];
  75. [More release];
  76. }
  77. -(Void) appendTableWith :( NSMutableArray *) data
  78. {
  79. For (int I = 0; I <[data count]; I ++ ){
  80. [Items addObject: [data objectAtIndex: I];
  81. }
  82. NSMutableArray * insertIndexPaths = [NSMutableArray arrayWithCapacity: 10];
  83. For (int ind = 0; ind <[data count]; ind ++ ){
  84. NSIndexPath * newPath = [NSIndexPath indexPathForRow: [items indexOfObject: [data objectAtIndex: ind] inSection: 0];
  85. [InsertIndexPaths addObject: newPath];
  86. }
  87. [Self. myTableView insertRowsAtIndexPaths: insertIndexPaths withRowAnimation: UITableViewRowAnimationFade];
  88. }
  89. @ 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!

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.