我喜欢在桌子上显示100条记录。但我只会显示10条记录,当用户点击最后一行(Which will have a button that will say load the next 10 records)
时,该表将排列接下来的10条记录。
我已成功显示最后一个单元格上的更多按钮。但我不知道如何一次显示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];
以上内容将在显示所有More
条记录后显示100
按钮。
moreButtonClickAction
-(void)moreButtonClickAction {
NSLog(@"Suppose to load the next 10 records");
}
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arrayThatContainsAllHundredRecords count];
}
我需要一种编辑此代码的方法,因此我一次只会显示10条记录,并在用户点击more
按钮时显示下一条记录。 (现在总记录应为20 - (前10个+单击more
按钮后的10条记录))
注意:我将下载所有100条记录并将其保存在self.arrayThatContainsAllHundredRecords
方法的viewDidLoad
中。
我相信我必须在执行[self.tableView reloadData];
之前做一些更改,就像在tell中加载10条记录一样。
好吧,如果我只有53条记录。然后用户将单击更多按钮5次,在第6个实例上,该表应仅显示3条记录。如何在tableview中处理:numberOfRowsInSection:?
答案 0 :(得分: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];
}
答案 1 :(得分: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;
}