将记录加载到TableView - (更多按钮)

问题描述:

我有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

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

要显示在时间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