[IOS]上拉显示更多数据例子(0 位领导批示)
- 2011-12-21
- 分类:IOS
- 作者:银子
- 261 位领导视察
最近研究了一下汽车之家手机客户端的上拉显示更多数据功能,写了一个简单的例子。
创建一个新项目,在rootView.h中加入下列代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | @interface RootViewController : UITableViewController { // 表格数据数组,因为是演示代码,直接定义为数组 NSMutableArray *tableData; // 下拉时显示的数据 NSMutableArray *tableMoreData; // 数据数量 NSUInteger dataNumber; // 加载状态 BOOL _loadingMore; } // 表格视图,需要与xib中的tableView关联 @property(nonatomic, retain) IBOutlet UITableView *tableView; // 创建表格底部 - (void) createTableFooter; // 开始加载数据 - (void) loadDataBegin; // 加载数据中 - (void) loadDataing; // 加载数据完毕 - (void) loadDataEnd; @end |
然后在 rootView.m 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | @implementation RootViewController @synthesize tableView = _tableView; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSLocalizedString(@"Master", @"Master"); } return self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. tableData = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"One",@"Two",@"Three",@"Four",@"One",@"Two",@"Three",@"Four",nil]; tableMoreData = [[NSMutableArray alloc] initWithObjects:@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",nil]; [self createTableFooter]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [tableData count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } // Configure the cell. cell.textLabel.text = [tableData objectAtIndex:indexPath.section]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { // 下拉到最底部时显示更多数据 if(!_loadingMore && scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.size.height))) { [self loadDataBegin]; } } // 开始加载数据 - (void) loadDataBegin { if (_loadingMore == NO) { _loadingMore = YES; UIActivityIndicatorView *tableFooterActivityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(75.0f, 10.0f, 20.0f, 20.0f)]; [tableFooterActivityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; [tableFooterActivityIndicator startAnimating]; [self.tableView.tableFooterView addSubview:tableFooterActivityIndicator]; [self loadDataing]; } } // 加载数据中 - (void) loadDataing { dataNumber = [tableData count]; for (int x = 0; x < [tableMoreData count]; x++) { [tableData addObject:[tableMoreData objectAtIndex:x]]; } [[self tableView] reloadData]; [self loadDataEnd]; } // 加载数据完毕 - (void) loadDataEnd { _loadingMore = NO; [self createTableFooter]; } // 创建表格底部 - (void) createTableFooter { self.tableView.tableFooterView = nil; UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 40.0f)]; UILabel *loadMoreText = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 116.0f, 40.0f)]; [loadMoreText setCenter:tableFooterView.center]; [loadMoreText setFont:[UIFont fontWithName:@"Helvetica Neue" size:14]]; [loadMoreText setText:@"上拉显示更多数据"]; [tableFooterView addSubview:loadMoreText]; self.tableView.tableFooterView = tableFooterView; } @end |
1 2 3 4 5 6 7 8 9 10 11 | - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. tableData = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"One",@"Two",@"Three",@"Four",@"One",@"Two",@"Three",@"Four",nil]; tableMoreData = [[NSMutableArray alloc] initWithObjects:@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",nil]; [self createTableFooter]; } |
视图加载完成后,给两个数组赋值,并创建了 tableFooterView
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 创建表格底部 - (void) createTableFooter { self.tableView.tableFooterView = nil; UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 40.0f)]; UILabel *loadMoreText = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 116.0f, 40.0f)]; [loadMoreText setCenter:tableFooterView.center]; [loadMoreText setFont:[UIFont fontWithName:@"Helvetica Neue" size:14]]; [loadMoreText setText:@"上拉显示更多数据"]; [tableFooterView addSubview:loadMoreText]; self.tableView.tableFooterView = tableFooterView; } |
关键的实现部分:
1 2 3 4 5 6 7 8 | - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { // 下拉到最底部时显示更多数据 if(!_loadingMore && scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.size.height))) { [self loadDataBegin]; } } |
scrollViewDidEndDragging在停止拖拽的时候开始执行, 当用户拖拽视图到最底部时,执行 loadDataBegin。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // 开始加载数据 - (void) loadDataBegin { if (_loadingMore == NO) { _loadingMore = YES; UIActivityIndicatorView *tableFooterActivityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(75.0f, 10.0f, 20.0f, 20.0f)]; [tableFooterActivityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; [tableFooterActivityIndicator startAnimating]; [self.tableView.tableFooterView addSubview:tableFooterActivityIndicator]; [self loadDataing]; } } |
当_loadingMore 为 NO 时这个方法才会执行, 方法中给表格底部视图加上了一个预加载的图标ActivityIndicator(小菊花),并执行loadDataing。BTW:其实这两个方法可以合并的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // 加载数据中 - (void) loadDataing { dataNumber = [tableData count]; for (int x = 0; x < [tableMoreData count]; x++) { [tableData addObject:[tableMoreData objectAtIndex:x]]; } [[self tableView] reloadData]; [self loadDataEnd]; } |
这个方法中先统计数组(表格数据源)大小,然后将新增数据 add 到这个数组中,最后 reloadData 表格视图,并执行loadDataEnd
1 2 3 4 5 6 | // 加载数据完毕 - (void) loadDataEnd { _loadingMore = NO; [self createTableFooter]; } |
结束加载数据 _loadingMore 设为 NO,重新创建 tableFooterView。
打完收工。
相关文章
NOTE:本博内容大部分为原创,转载请注明出处。
永久链接:http://www.zdyi.com/ios-demo-load-more-data/656
![[IOS学习]使用URLRequest方法向服务器端发送数据](http://www.zdyi.com/wp-content/plugins/silver-related-posts/nopicture.gif)

























银子曰:还没有领导题词?
请领导指示
Additional comments powered by BackType