本文共 2240 字,大约阅读时间需要 7 分钟。
UITableView分页上拉加载简单,ARC环境,源码如下,以作备份:
原理是,点击最后一个cell,触发一个事件来处理数据,然后reloadData
RootViewController.m + RootViewController.h
#import "RootViewController.h"@interface RootViewController ()@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSMutableArray *dataSource;@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; _dataSource = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i++) { [_dataSource addObject:[NSString stringWithFormat:@"%d", i]]; } _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 因为要定制最后一行,所以cell多一个 return _dataSource.count + 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reusedStr = @"demo"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedStr]; } if([indexPath row] == ([_dataSource count])) { // 定制最后一行的cell cell.textLabel.text=@"获取更多.."; } else { // 定制普通行的cell cell.textLabel.text=[_dataSource objectAtIndex:[indexPath row]]; } return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 如果是最后一行点击事件,则触发一个事件 if (indexPath.row == ([_dataSource count])) { [self performSelectorInBackground:@selector(loadMore) withObject:nil]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; return; }}- (void)loadMore{ // 添加数据源 for (int i = 0; i < 10; i++) { [_dataSource addObject:[NSString stringWithFormat:@"新%d", i]]; } // 重新加载tableView [_tableView reloadData];}@end
#import@interface RootViewController : UIViewController@end
转载地址:http://fgall.baihongyu.com/