iPhone中使用UITableView實(shí)現(xiàn)分頁效果(附代碼)
iPhone中使用UITableView實(shí)現(xiàn)分頁效果是本文要介紹的內(nèi)容,UITableview 能夠列表顯示許多內(nèi)容,也是我們開發(fā)中經(jīng)常用的一個(gè)組件。我們經(jīng)常會(huì)分頁顯示列表,如先顯示 10 條記錄,點(diǎn)擊更多在添加 10 條,以此類推,下面是實(shí)現(xiàn)類似更多顯示的一個(gè) demo。
實(shí)現(xiàn)的效果如下:
點(diǎn)擊 “More…”,實(shí)現(xiàn)后面的效果.
實(shí)現(xiàn)的思路:
基本上就是數(shù)據(jù)源里先只放10條, 點(diǎn)擊***一個(gè)cell時(shí), 添加更多的數(shù)據(jù)到數(shù)據(jù)源中。
處理"加載更多"的那個(gè)cell的選擇事件,觸發(fā)一個(gè)方法來加載更多數(shù)據(jù)到列表。
indexPathForRow插入數(shù)據(jù)。
實(shí)現(xiàn)過程如下:
- #import <UIKit/UIKit.h>
- @interface iphone_tableMoreViewController : UIViewController
- <UITableViewDelegate,UITableViewDataSource>{
- IBOutlet UITableView *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 {
- [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])) {
- //創(chuàng)建loadMoreCell
- cell.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]];
- }
- //加載你的數(shù)據(jù)
- [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
源代碼:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.tableMore/
小結(jié):iPhone中使用UITableView實(shí)現(xiàn)分頁效果(附代碼)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)對(duì)你有所幫助!