UITableView加载更多显示
来源:互联网 发布:freertos源码 编辑:程序博客网 时间:2024/06/05 04:46
源代码:源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.tableMore/
//// iphone_tableMoreViewController.m// iphone.tableMore//// Created by wangjun on 11-3-17.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "iphone_tableMoreViewController.h"@implementation iphone_tableMoreViewController@synthesize items,myTableView;- (void)viewDidLoad { [super viewDidLoad];items=[[NSMutableArray alloc] initWithCapacity:0];for (int i=0; i<10; i++) {[items addObject:[NSString stringWithFormat:@"cell %i",i]];}}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}- (void)viewDidUnload {items=nil;self.myTableView=nil;}- (void)dealloc {[self.myTableView release];[items release]; [super dealloc];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {int count = [items count]; return count + 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *tag=@"tag";UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tag];if (cell==nil) {cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:tag] autorelease];} if([indexPath row] == ([items count])) { //鍒涘缓loadMoreCellcell.textLabel.text=@"More.."; }else {cell.textLabel.text=[items objectAtIndex:[indexPath row]];} return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == [items count]) {UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath];loadMoreCell.textLabel.text=@"loading more ..."; [self performSelectorInBackground:@selector(loadMore) withObject:nil]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; return; } //鍏朵粬cell鐨勪簨浠�}-(void)loadMore{ NSMutableArray *more; more=[[NSMutableArray alloc] initWithCapacity:0];for (int i=0; i<10; i++) {[more addObject:[NSString stringWithFormat:@"cell ++%i",i]];}//鍔犺浇浣犵殑鏁版嵁 [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO];[more release];}-(void) appendTableWith:(NSMutableArray *)data{ for (int i=0;i<[data count];i++) { [items addObject:[data objectAtIndex:i]]; } NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10]; for (int ind = 0; ind < [data count]; ind++) { NSIndexPath *newPath = [NSIndexPath indexPathForRow:[items indexOfObject:[data objectAtIndex:ind]] inSection:0]; [insertIndexPaths addObject:newPath]; } [self.myTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];}@end