当Facebook应用程序滚动到底部时,UITableView加载更多 [英] UITableView load more when scrolling to bottom like Facebook application
问题描述
我正在开发一个使用SQLite的应用程序。我想使用分页机制显示用户列表(UITableView)。任何人都可以告诉我当用户滚动到列表的末尾时(如在Facebook应用程序的主页上)如何在我的列表中加载更多数据?
I am developing an application that uses SQLite. I want to show a list of users (UITableView) using a paginating mechanism. Could any one please tell me how to load more data in my list when the user scrolls to the end of the list (like on home page on Facebook application)?
推荐答案
您可以通过在 cellForRowAtIndexPath中添加对您所在位置的检查来实现:
方法。这个方法很容易理解和实现:
You can do that by adding a check on where you're at in the cellForRowAtIndexPath:
method. This method is easy to understand and to implement :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Classic start method
static NSString *cellIdentifier = @"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MainMenuCellIdentifier];
}
MyData *data = [self.dataArray objectAtIndex:indexPath.row];
// Do your cell customisation
// cell.titleLabel.text = data.title;
BOOL lastItemReached = [data isEqual:[[self.dataArray] lastObject]];
if (!lastItemReached && indexPath.row == [self.dataArray count] - 1)
{
[self launchReload];
}
}
编辑:添加了对最后一项的检查以防止递归调用。你必须实现定义是否已达到最后一项的方法。
EDIT : added a check on last item to prevent recursion calls. You'll have to implement the method defining whether the last item has been reached or not.
EDIT2:解释lastItemReached
EDIT2 : explained lastItemReached
这篇关于当Facebook应用程序滚动到底部时,UITableView加载更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!