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

标签 iphone objective-c cocoa-touch

我有大约 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记录。

更多按钮点击操作

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

节内行数

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

    return [self.arrayThatContainsAllHundredRecords count];
}

我需要一种方法来编辑此代码,因此我一次只会显示 10 条记录,并在用户单击 more 时显示下 10 条记录。按钮。 (现在总记录应为 20 -(前 10 条 + 单击 more 按钮后的 10 条记录))

注意:我将下载所有 100 条记录并将其保存在 self.arrayThatContainsAllHundredRecords 中在 viewDidLoad方法。

我相信在执行 [self.tableView reloadData]; 之前我必须做出一些改变,如告诉加载 10 条记录。

好吧,假设我只有 53 条记录。然后用户将单击“更多”按钮 5 次,在第 6 次时,表应仅显示 3 条记录。如何在 tableview:numberOfRowsInSection: 中处理这个问题?

最佳答案

tableView:numberOfRowsInSection: 是确定将显示多少行的方法,因此第一步是更改它。您还需要 View Controller 中的一个属性来跟踪应该可见的行数。像这样的事情:

- (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];
}

关于iphone - 将记录加载到 TableView -(“更多”按钮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9179083/

相关文章:

iphone - 在 iPhone OS 中访问地址簿

ios - tableview 在使用 xib 单元格时发现 nil

iPhone:如何在 Tabbar 应用程序中的多个 View Controller 之间传递数据

iphone - 在 Objective-C 中分配/初始化实例变量的正确方法?

objective-c - 为什么 DTrace 有时但不总是给我无效地址错误?

Objective-c 中的 SQL 和 oop

iphone - 创建 "Add" View Controller 的最佳方法

iphone - 以与邮件应用程序相同的方式显示/隐藏 UITableView header (UITextField)

iphone - 为什么我的 UITextField 的字体在编辑时会改变?

iphone - 是否可以使用其他应用程序(例如 Skype)打开 iPhone 电话号码链接或更改默认拨号器?