2012-02-07 49 views
2

我有100個記錄要顯示在表上。但是我只會顯示10條記錄,並且當用戶單擊最後一行(Which will have a button that will say load the next 10 records)時,該表將對接下來的10條記錄進行排序。將記錄加載到TableView - (更多按鈕)

我已經在最後一個單元上成功顯示了更多按鈕。但我不知道如何一次顯示10條記錄。我的代碼到目前爲止;

viewDidLoad中

self.arrayThatContainsAllHundredRecords= [NSArray arrayWithArray:hundredRecordsArray]; 

[self performSelector:@selector(addTheTableFooter:) withObject:nil afterDelay:0.5]; 

addTheTableFooter

UIView* footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 

[footer addSubview:moreButton]; // i have also added an event to the button click see below, and i have not shown it here. 

self.tableView.tableFooterView =footer; 

[self.mainWindow removeFromSuperview]; 

[self.tableView reloadData]; 

上面將顯示所有記錄100後顯示More按鈕。

moreButtonClickAction

-(void)moreButtonClickAction { 
NSLog(@"Suppose to load the next 10 records"); 
} 

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [self.arrayThatContainsAllHundredRecords count]; 
} 

我需要一種方法來修改這個代碼,所以我將只顯示10條記錄的時間,並顯示下一個10時用戶單擊more按鈕。 (現在總的記錄應該是20 - (先前的10點+ 10點的記錄爲點擊more按鈕)的結果)

注:我會下載所有100條記錄,並在viewDidLoad方法保存它self.arrayThatContainsAllHundredRecords

我相信在執行[self.tableView reloadData];之前,我必須做一些修改,就像加載10條記錄一樣。

那麼,如果我只有53條記錄。然後用戶將單擊更多按鈕5次,在第6次實例中,該表應該只顯示3條記錄。這怎麼可以在tableview中處理:numberOfRowsInSection:?

+0

等待,所以當第一個10行是可見的,用戶按下按鈕來顯示更多的紀錄後,在這一點上是可見的?前20條記錄,或只記錄11-20條?在我的回答中,我假設前者,而MusiGenesis正在迴應後者。 – yuji 2012-02-07 15:58:10

+0

應該可見第10條記錄+第2條10條記錄。現在應該總共有20條記錄 – sharon 2012-02-07 16:03:35

回答

2

tableView:numberOfRowsInSection:是確定將顯示多少行的方法,因此第一步將改變它。您還需要視圖控制器中的屬性來跟蹤應該顯示多少行。這樣的事情:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.numberOfRowsVisible; 
} 

而當更多的按鈕被點擊,你必須增加這個數字,並重新加載tableView數據。

-(void)moreButtonClickAction { 
    if (self.numberOfRowsVisible < self.maxNumberOfRows) 
     self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 10, self.maxRows); 
    [self.tableView reloadData]; 
} 
+0

我明白了。但如果我們有53條記錄,會發生什麼。我們在'moreButtonClickAction'事件中硬編碼10。當用戶第六次點擊時會出現異常。我們怎麼能解決這個問題(對不起,我之前沒有提到過這個問題) – sharon 2012-02-07 15:57:57

+0

看我的編輯:現在應該很好。 – yuji 2012-02-07 16:01:16

+0

最初,當視圖加載數據時,那麼'self.numberOfRowsVisible'是什麼?我們最初是否給它一個價值? – sharon 2012-02-07 16:20:19

1

要顯示在時間10行,您的tableview:numberOfRowsInSection:應返回(而非源陣列中的返回記錄的總數)10。

然後,您的tableView:cellForRowAtIndexPath:應該處理分頁,將當前頁面時間添加到indexPath.row屬性中,以確定源數組中的哪個元素用於饋送單元視圖。

當您點擊「更多」按鈕時,您應該在那裏撥打[self.tableView reloadData];

編輯:cellForRowAtIndexPath:方法應該是這個樣子:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"TesterCell"; 

    UITableViewCell *cell = [tableView 
     dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSInteger dataIndex = indexPath.row + (pageNumber * 10); 
    MyDataObject *data = (MyDataObject *)[self.arrayThatContainsAllHundredRecords 
     objectAt:dataIndex]; 

    NSString *cellText = data.LastName; // or whatever 

    cell.textLabel.text = cellText; 

    return cell; 
} 
+0

好吧,比方說,如果我只有53條記錄。然後用戶將單擊更多按鈕5次,在第6次實例中,該表應該只顯示3條記錄。這怎麼可以在'tableview:numberOfRowsInSection:'(我沒有提到這個問題 - 對不起)處理?我不明白我應該在'cellForRowAtIndexPath'事件中做什麼。你能否添加一些細節? 'pageNumber'從哪裏來的? – sharon 2012-02-07 15:54:18

+0

? – sharon 2012-02-07 16:09:31