在網上搜尋了一下, 介紹UITableView分頁的文章不少, 而且都很統一, 代碼也都正確. 只是沒有把思路給整理出來. 我這裡借花獻佛, 整理一下.
這裡假定的前提是, 你已經將UITableView添加到了View中, 並且在h檔案中實現了 UITableViewDelegate,UITableViewDataSource 這兩個介面, 且已經與你後台定義的tblView建立起了關聯, UITableView的datesource和delegete也都已經指向了file's owner.
如果此處不太明白的話, 建議還是複習一下如何使用UITableView, 再來研究這部份.
1. 首先需要做的是, 定義資料來源. UITableView是需要一個資料來源的, 我這用使用的是SQLITE資料庫, 因此做了一個小小的分頁查詢. SQLIte的分頁查詢與MySQL的相同.
select * from table where 列名 = 條件 limit 頁數 * 每頁顯示記錄數, 每頁顯示記錄數
+(NSMutableArray *)GetRecord:(NSInteger)p{ //代碼中, 除了SQLITE的SELECT操作之外, 和分頁有關係的就是參數p和下面分頁的SQL文法拼接形式了. NSString *query = [NSString stringWithFormat:@"select * from table order by ID limit %d,10", (p-1) * 10]; char *select =(char *)[query UTF8String]; NSMutableArray *array = [[NSMutableArray alloc] init]; sqlite3 *database; if(sqlite3_open([DbObject GetDatabasePath], &database) == SQLITE_OK) { sqlite3_stmt *statement; if(sqlite3_prepare_v2(database, select, -1, &statement, nil)== SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { //根據欄位的類似, 使用sqlite3_column_init, sqlite3_column_blob, sqlite3_column_text等將資料從屬記錄行中取出來. 此處代碼略 //然後將出來的值,以索引值對應的形式賦值給NSDictionary數組. NSDictionary *rowRecord = [[NSDictionary alloc] initWithObjectsAndKeys:nsID,@"ID", data, @"Image", nsMessage, @"Message", nsVideoURL, @"VideoURL", nsAudioURL, @"AudioURL", nsToLine, @"ToLine",nsSendDate,@"SendDate", nil]; //將NSDictionary添加到NSMuableArray數組中. [array addObject:rowRecord]; } sqlite3_finalize(statement); } sqlite3_close(database); } return array;}
當然了這個函數還要在你的h檔案中聲明, 然後才可以在m檔案中implement,否則當你使用 [類名 函數名:參數] 訪問時會找不到方法的.
2. 這下要進入我們的頁面進行設計了. 首先要在頁面的.h 檔案中聲明變數 NSInteger currentPage, 這個變數是用來告訴系統, 我現在是處於第幾頁.
NSInteger currentPage;@property(strong, nonatomic) IBOutlet NSMutableArray *listData;@property(strong, nonatomic) IBOutlet UITableView *tbView;
3. 在系統初始化時, 將變數的值設定為1, 然後對UITableView對象使用到的資料來源進行賦值.
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib. currentPage = 1; listData = [DbMyClass GetRecord:currentPage];}
4. 然後我們要做的就是處理UITableView了, 正常情況下, 我們在numOfRowsInSections的地方返回的是總記錄數, 但是我們多加了行分頁按鈕, 因此返回的總記錄數要加上1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSInteger row = [listData count]; return row +1;}
5. 然後我們在 cellForRowAtIndexPath 中正常處理我們的儲存格資訊, 比如說我們使用了自訂儲存格, 我們就要在這裡載入... 這是最重要的一點是, 需要判斷 [indexPathrow] =和[listData
count]的值是否相同, 如果相同, 那麼就需要載入我們的分頁按鈕了.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellTableIdentifier = @"CusCellFutureMessage"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier]; if(cell == nil) { if([indexPath row] == [listData count]) { //建立一個儲存格, 並且將其樣式調整成我們需要的樣子. cell=[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"LoadMoreIdentifier"]; cell.font = [UIFont boldSystemFontOfSize:13]; cell.textLabel.text = @"讀取更多..."; } else { //其它儲存格的初始化事件. 自訂儲存格的讀取, 或者是儲存格的設定等. } } return cell;}
6. 然後就是註冊UITableView被點擊之後, 要觸發的事件了. 事件需要在 didSelectRowAtIndexPath 中定義
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if([indexPath row] == [listData count]) { UITableViewCell *loadMoreCell=[tableView cellForRowAtIndexPath:indexPath]; loadMoreCell.textLabel.text=@"正在讀取更資訊 …"; [self performSelectorInBackground:@selector(loadMore) withObject:nil]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; return; } else { //其它儲存格的事件 }}
7. 也許你注意到了, 在給儲存格設定peformSelectorInBackground的時候, 我們調用了loadMore函數. 這個函數就是我們這裡的第二核心的方法了.
-(void)loadMore { //當你按下這個按鈕的時候, 意味著你需要看下一頁了, 因此當前頁碼加1 currentPage ++; NSMutableArray *more = [DbMyClass GetRecord:currentPage]; //通過調用GetRecord方法, 將資料取出. [self performSelectorOnMainThread:@selector(appendTableWith:) withObject:more waitUntilDone:NO]; } -(void) appendTableWith:(NSMutableArray *)data { //將loadMore中的NSMutableArray添加到原來的資料來源listData中. for (int i=0;i<[data count];i++) { [listData addObject:[data objectAtIndex:i]]; } NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10]; for (int ind = 0; ind < [data count]; ind++) { NSIndexPath *newPath = [NSIndexPath indexPathForRow:[listData indexOfObject:[data objectAtIndex:ind]] inSection:0]; [insertIndexPaths addObject:newPath]; } //重新調用UITableView的方法, 來產生行. [self.tbView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade]; }
這些函數看著挺多的, 其實仔細看看也並不複雜, 當你實現了UITableView的介面後, 除了 loadMore, appendTableWitdh 以及定義的分頁函數GetRecord, 其餘的都是在實現UITableView的介面中定義的方法.