parse.com PFQueryTableViewController正在多次返回相同的对象
-
20-12-2019 - |
题
我正在尝试使用 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
自定义显示 PFObject
数据在a PFQueryTableViewController
.所以我构造了我的 PFQuery
对象在 (PFQuery *)queryForTable
在我的 PFQueryTableViewController
delegate
这应该是在一个 NSArray
.
我以为parse.com 应该然后发送/呼叫 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
每个返回的对象一次。但是,我发现它不断返回 同 对象多次。
因此,例如,代替3个不同的对象为3次 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
被调用,我得到相同的对象。但它不是数据或查询的问题,因为在结束 (PFQuery *)queryForTable
,就在之前
return query;
我试过了
[query findObjectsInBackgroundWithBlock:^(NSArray *resultsNow,NSError *error) {NSLog(@"We have the following: %@", resultsNow);}];
sleep(5);
这显示了正确获得的3个对象。怎么会这样呢? 同 处理时的查询 PFQueryTableViewController
打电话来 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
3次,而不是将3个对象逐个发送,而是将第一个发送3次?
为了回答Wain的问题,我在评论中添加了一些答案,但以下内容似乎是相关的:
- (PFObject *)objectAtIndex:(NSIndexPath *)indexPath {
// overridden, since we want to implement sections
if (indexPath.section < self.objects.count) {
return [self.objects objectAtIndex:indexPath.section];
}
return nil;
}
解决方案 2
感谢Wain的问题,我记录了objectsDidLoad:并且发现正确的3个对象正在加载,所以它必须是PFQueryTableViewController的问题,只是没有按照正确的顺序加载它们 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
.
特别是,映射应该是在 -(PFObject *)objectAtIndex:(NSIndexPath *)indexPath
正如我的问题所示。但是在第N次查看文档时,我突然注意到方法名称实际上应该是 -(PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
.难怪它没有被调用,我的映射正在生效,因此错误的对象被传递给 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
.当我把它改成 -(PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
, ,问题消失了!
我怎么会得到错误的方法名称?我发现我已经从anypic代码复制了那个映射示例,所以我猜测parse.com 在anypic编写后的某个时候更改了方法名称?任何人都可以验证吗?我正在使用parse framework1.2.14
其他提示
我有一个类似的问题,我所做的是:
1-而不是使用 PFQueryTableViewController
只需使用常规 UITableViewController
.
2-更改方法 queryForTable:
到 (void)
.
- (void)queryForTable {
3-删除 PFQueryTableView
方法和实现 DataSource
方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
打电话 [self queryForTable]
在 viewDidLoad
在宣布进去之后。h文件。
如果你不使用块,你只需创建 NSArray
property
并填充 queryForTable
:
self.yourarray = [query findobjects];
如果你使用块,只要确保你填充 NSArray
内块并重新加载tableview。
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
// There was an error, do something with it.
}
else {
self.yourArray = objects;
}
// Reload your tableView Data
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
这就是我工作的原因。希望有帮助。