以前,写了一篇文章:下拉刷新的实现,这篇文章提供了另一种解决方案。
核心代码请参考附件。
DemoTableViewController.h
#import <UIKit/UIKit.h> #import "PullRefreshTableViewController.h" @interface DemoTableViewController : PullRefreshTableViewController { NSMutableArray *items; } @end
DemoTableViewController.m
#import "DemoTableViewController.h" @implementation DemoTableViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Pull to Refresh"; items = [[NSMutableArray alloc] initWithObjects:@"What time is it?", nil]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [items count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = [items objectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void)refresh { [self performSelector:@selector(addItem) withObject:nil afterDelay:2.0]; } - (void)addItem { NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; NSString *now = [dateFormatter stringFromDate:[NSDate date]]; [items insertObject:[NSString stringWithFormat:@"%@", now] atIndex:0]; [self.tableView reloadData]; [self stopLoading]; } - (void)dealloc { [items release]; [super dealloc]; } @end