// // CustomTableView.m // HD wallpaper #import "CustomTableView.h" @interface CustomTableView () @property (nonatomic, strong) UIView *loadMoreFooterView; @property (nonatomic, assign) BOOL isLoadingMore; @end @implementation CustomTableView - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style { self = [super initWithFrame:frame style:style]; if (self) { [self commonInit]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; [self commonInit]; } - (void)commonInit { self.dataSource = self; self.delegate = self; // 添加刷新控制 self.refreshControl = [[UIRefreshControl alloc] init]; [self.refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged]; [self addSubview:self.refreshControl]; // 添加加载更多视图 self.loadMoreFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)]; UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium]; activityIndicator.center = self.loadMoreFooterView.center; [activityIndicator startAnimating]; [self.loadMoreFooterView addSubview:activityIndicator]; self.tableFooterView = self.loadMoreFooterView; self.isLoadingMore = NO; } - (void)refreshTable { if (self.onRefresh) { self.onRefresh(); } } - (void)endRefreshing { [self.refreshControl endRefreshing]; } - (void)endLoadingMore { self.isLoadingMore = NO; self.tableFooterView.hidden = YES; } #pragma mark - UITableViewDataSource & UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 你需要实现的数据源方法 return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 你需要实现的数据源方法 return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height - 100) { if (!self.isLoadingMore && self.onLoadMore) { self.isLoadingMore = YES; self.tableFooterView.hidden = NO; self.onLoadMore(); } } } @end //#import "CustomTableView.h" // //@interface ViewController () // //@property (nonatomic, strong) CustomTableView *tableView; //@property (nonatomic, strong) NSMutableArray *data; // //@end // //@implementation ViewController // //- (void)viewDidLoad { // [super viewDidLoad]; // // self.data = [NSMutableArray array]; // [self loadData]; // // self.tableView = [[CustomTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; // [self.view addSubview:self.tableView]; // // __weak typeof(self) weakSelf = self; // self.tableView.onRefresh = ^{ // [weakSelf refreshData]; // }; // self.tableView.onLoadMore = ^{ // [weakSelf loadMoreData]; // }; //} // //- (void)loadData { // // 加载数据 // for (int i = 0; i < 20; i++) { // [self.data addObject:[NSString stringWithFormat:@"Item %d", i]]; // } // [self.tableView reloadData]; //} // //- (void)refreshData { // // 模拟刷新 // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [self.data removeAllObjects]; // [self loadData]; // [self.tableView endRefreshing]; // }); //} // //- (void)loadMoreData { // // 模拟加载更多 // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // NSInteger currentCount = self.data.count; // for (int i = 0; i < 20; i++) { // [self.data addObject:[NSString stringWithFormat:@"Item %ld", currentCount + i]]; // } // [self.tableView reloadData]; // [self.tableView endLoadingMore]; // }); //} // //@end