实现的效果如下:self.myTableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationF
实现的效果如下:

点击 “More…”,实现后面的效果.
- 基本上就是数据源里先只放10条, 点击最后一个cell时, 添加更多的数据到数据源中.。
- 处理"加载更多"的那个cell的选择事件,触发一个方法来加载更多数据到列表。
- indexPathForRow插入数据。
#import<UIKit/UIKit.h>
@interfaceiphone_tableMoreViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
IBOutletUITableView *myTableView;
NSMutableArray *items;
}
@property (nonatomic,retain) UITableView *myTableView;
@property (nonatomic,retain) NSMutableArray *items;
@end#import"iphone_tableMoreViewController.h"
@implementation iphone_tableMoreViewController
@synthesize items,myTableView;
- (void)viewDidLoad {
[superviewDidLoad];
items=[[NSMutableArray alloc] initWithCapacity:0];
for (inti=0; i<10; i++) {
[items addObject:[NSString stringWithFormat:@"cell %i",i]];
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}- (void)viewDidUnload{
items=nil;
self.myTableView=nil;
}
- (void)dealloc {
[self.myTableView release];
[itemsrelease];
[superdealloc];
}-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {
int count =[items count];
return count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *tag=@"tag";
UITableViewCell *cell=[tableViewdequeueReusableCellWithI dentifier:tag];
if(cell==nil) {
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:tag] autorelease];
}
if([indexPath row] == ([items count])) {
//创建loadMoreCell
cell.textLabel.text=@"More..";
}else{
cell.textLabel.text=[items objectAtIndex:[indexPathrow]];
}
returncell;
}
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [items count]) {
UITableViewCell *loadMoreCell=[tableViewcellForRowAtIndexPath:indexPath];
loadMoreCell.textLabel.text=@"loading more …";
[self performSelectorInBackgro und:@selector(loadMore)withObject:nil];
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
return;
}
//其他cell的事件
}
-(void)loadMore
{
NSMutableArray *more;
more=[[NSMutableArray alloc] initWithCapacity:0];
for (inti=0; i<10; i++) {
[more addObject:[NSString stringWithFormat:@"cell ++%i",i]];
}
//加载你的数据
[selfperformSelectorOnMainThr ead:@selector(appendTableWith:)withObject:more waitUntilDone:NO];
[morerelease];
}
-(void) appendTableWith:(NSMutableArray *)data
{
for (inti=0;i<[data count];i++) {
[items addObject:[data objectAtIndex:i]];
}
NSMutableArray *insertIndexPaths = [NSMutableArrayarrayWithCapacity:10];
for (int ind= 0; ind < [data count]; ind++) {
NSIndexPath *newPath = [NSIndexPath indexPathForRow:[itemsindexOfObject:[data objectAtIndex:ind]] inSection:0];
[insertIndexPaths addObject:newPath];
}
[self.myTableViewinsertRowsAtIndexPaths:insertIndexPathswithRowAnimation:UITableViewRowAnimationF ade];
}
@end
这篇博客介绍了如何在iOS应用中利用UITableView实现分页加载更多数据的效果。通过初始加载10条数据,当用户点击'加载更多'的单元格时,后台加载额外的10条数据并更新表格视图。
1034

被折叠的 条评论
为什么被折叠?



