Ios 自动分页PFQueryTableViewController
我的目标是使用Ios 自动分页PFQueryTableViewController,ios,objective-c,parse-platform,Ios,Objective C,Parse Platform,我的目标是使用PFQueryTableViewController设置分页。我对以下解决方案之一感到满意: 1、自动分页表视图。与示例一样,如果objectsPerPage=10tableView在实际行为10时加载接下来的10个对象。划船等等 2、使用“加载更多单元格”进行分页UITableViewCell。在我看来,这是一种流行的解决方案,所以我也喜欢这种变化 事实上,当我尝试第二种解决方案时,我没有得到任何错误,它根本不起作用。我在故事板上添加了一个新单元,并为其创建了一个类,并检查了
PFQueryTableViewController
设置分页。我对以下解决方案之一感到满意:
- 1、自动分页表视图。与示例一样,如果
tableView在实际行为10时加载接下来的10个对象。划船等等objectsPerPage=10
- 2、使用“加载更多单元格”进行分页
。在我看来,这是一种流行的解决方案,所以我也喜欢这种变化UITableViewCell
对象数时,我应该显示LoadMoreCell
单元格。count
。我尝试了几种方法,但都不起作用,反正我无法显示LoadMoreCell
以下是我的尝试:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row < self.objects.count) {
NSLog(@"THE NUMBER OF ACTUAL ROW %d", indexPath.row);
NSLog(@"cell tapped");
} else {
// load more
[self loadNextPage];
NSLog(@"LOAD NEXT PAGE");
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *LoadMoreCellIdentifier = @"loadmore";
LoadMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];
if (!cell) {
cell = [[LoadMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadMoreCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.loadMoreLabel.text = @"LOAD MORE CELLS";
NSLog(@"NO CELL");
}
return cell;
}
这是表视图设置
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.parseClassName = @"followClass";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 10;
}
return self;
}
- (PFQuery *)queryForTable {
if (![PFUser currentUser]) {
return nil;
}
PFQuery *followQuery = [PFQuery queryWithClassName:@"self.parseClassName"];
[followQuery whereKey:@"followed" equalTo:[PFUser currentUser]];
return query;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"followCell";
FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.followLabel.text = object[@"username"];
cell.avatar.file = object[@"image"];
[cell.avatar loadInBackground];
return cell;
}
下面是在我的测试项目中工作的代码。当未注释掉
numberofrowsinssection
时,我确实得到了您得到的错误。我确实看到加载更多单元格,但它会自动加载其余单元格,如果有帮助,请告诉我
#import "ParseDataTableViewController.h"
@implementation ParseDataTableViewController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom the table
// The className to query on
//self.parseClassName = @"ParseTest";
// The key of the PFObject to display in the label of the default cell style
// The title for this table in the Navigation Controller.
self.title = @"Test";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 15;
}
return self;
}
- (IBAction)nextPage:(id)sender
{
[self loadNextPageInTable];
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 15;
if(y > h + reload_distance) {
NSLog(@"load more rows");
[self loadNextPageInTable];
}
}
-(void) loadNextPageInTable {
[self loadNextPage];
NSLog(@"NEW PAGE LOADED");
}
- (PFQuery *)queryForTable {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name BEGINSWITH 't'"];
PFQuery *followQuery = [PFQuery queryWithClassName:@"ParseTest" predicate:predicate];
return followQuery;
}
/*-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"DataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = object[@"Name"];
return cell;
}
@end
我创建了第一个解决方案的演示应用程序。给你。我没有使用
PFQueryTableViewController
(仅使用UIKit),但我认为这不会有问题。在这个演示中,所有数据都是本地生成的,为了模拟下载时间,我添加了一个间隔为5s的计时器
错误:
由于未捕获的异常“nsinternalinconsistenceexception”而终止应用程序,原因是:“尝试从节0中删除第10行,该节在更新之前仅包含10行”
表示您正在尝试删除indexPath.row=10的项,但此位置上没有项。行从0开始计数,因此第10个(在本例中是最后一个)项具有indexath.row=9
我希望这能有所帮助。我对PFQueryTableViewController不太熟悉,但这里是我以前的一个项目中改编的工作解决方案:
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// totalObjectsCount - you need somehow to get this value, to stop loading next objects after you show all
if (self.objects.count < self.totalObjectsCount) {
return self.objects.count + 1;
} else {
return self.objects.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
if (self.objects.count < self.totalObjectsCount
&& indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1) {
LoadMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoadMoreCell" forIndexPath:indexPath];
return cell;
} else {
// better if you use [dequeueReusableCellWithIdentifier: forIndexPath:] instead of dequeueReusableCellWithIdentifier
FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FollowCell" forIndexPath:indexPath];
cell.followLabel.text = object[@"username"];
cell.avatar.file = object[@"image"];
[cell.avatar loadInBackground];
return cell;
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.objects.count < self.totalObjectsCount
&& indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1) {
// isLoadingMoreData - BOOL value, which indicating, that you already fetching new objects, we don't wanna make same multiple requests
if (!self.isLoadingMoreData) {
// loadMoreData - function, where you load more objects
[self loadMoreData];
}
}
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//totalObjectsCount—您需要以某种方式获取此值,以便在显示所有对象后停止加载下一个对象
if(self.objects.count
在单元格创建过程中,如果尚未创建新单元格,则tableview无法创建新单元格
当退出队列机制中不存在新单元时,创建新单元:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"followCell";
FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//adds new cell if one does not exist already
if (!cell) {
cell = [FeedTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//TODO: edit cell details
}
cell.followLabel.text = object[@"username"];
cell.avatar.file = object[@"image"];
[cell.avatar loadInBackground];
return cell;
}
@Yan我不确定它是否在
[self loadNextPage]
崩溃,因为我在queryForTable
中有一个NSLog
,它出现在NSLog(@“加载更多行”)之后的日志中代码>。因此我认为queryForTable
会被调用。否,没有用于删除行的选项。如果我把它注释掉,它会工作的。你能发布更多的代码吗。可能来自queryForTable以进一步调试和-(NSInteger)tableView:(UITableView*)tableView行数Section:(NSInteger)section@Yan我已经编辑了我的问题。一切看起来都很好。使用这段代码,您可以看到tableview中的所有对象,而不是10?如果您注释掉[self-LoadNextPaginTable];它不会崩溃,你看到分页了吗?加载表时,你看到的是10个对象还是所有的数据?不幸的是,它不工作,我得到了相同的错误。我认为如果您没有设置限制,并且在本例中我没有限制结果的数量,那么查询将加载所有结果。可能问题是单元格无法加载接下来的25个对象。没有主意。如果添加self.paginationEnabled=YES;这是默认设置,并注释掉整个-(void)scrollViewDidScroll:(UIScrollView*)asCollView方法,以查看是否可以使用默认分页。请告诉我您是否仍然存在问题。我创建了一个解析项目,如果需要,我会发布代码。当然。快乐编码!:)我检查了我正在使用此解决方案的应用程序,它不会返回。确保您在返回时没有重新加载表。在视图中的某个地方会出现什么。让我知道
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"followCell";
FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//adds new cell if one does not exist already
if (!cell) {
cell = [FeedTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//TODO: edit cell details
}
cell.followLabel.text = object[@"username"];
cell.avatar.file = object[@"image"];
[cell.avatar loadInBackground];
return cell;
}