【发布时间】:2014-01-06 14:24:56
【问题描述】:
我使用 parse.com 作为后端,我的查询非常慢,所以我试图将其更改为使用块。
基本上,我的查询用我需要的一切填充一个数组,根据 if 语句,我在块内调用方法,这些方法填充我将在cellForRowAtIndexPath 中使用的数组。问题是当我尝试reloadDatainside 块时,应用程序崩溃了。
代码如下:
- (void)queryForTable {
PFQuery *exerciciosQuery = [PFQuery queryWithClassName:@"ExerciciosPeso"];
[exerciciosQuery whereKey:@"usuario" equalTo:[PFUser currentUser]];
[exerciciosQuery includeKey:@"exercicio"];
exerciciosQuery.cachePolicy = kPFCachePolicyCacheElseNetwork;
[exerciciosQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[self configurarDatas];
_seriesArray = objects;
if (_seriesArray.count > 0) {
NSPredicate *predIniciante = [NSPredicate predicateWithFormat:@"serie contains [cd] %@", @"Ini"];
NSArray *arrayIniciante = [_seriesArray filteredArrayUsingPredicate:predIniciante];
NSArray *arrayInicianteApenasSeries = arrayIniciante;
NSArray *arrayInicianteApenasSeries2 = [arrayInicianteApenasSeries valueForKey:@"serie"];
NSSet *setInicianteApenasSeries = [NSSet setWithArray:arrayInicianteApenasSeries2];
NSArray *arrayInicianteCount = [setInicianteApenasSeries allObjects];
if (arrayInicianteCount.count > 0) {
[self popularSeriesInicianteAB];
// [self.tableView reloadData];
}
else if (arrayInicianteCount.count > 8) {
[self popularSeriesInicianteC];
// [self.tableView reloadData];
}
else {
[self popularSeriesAvancado];
// [self.tableView reloadData];
NSLog(@"POPULAR SERIES AVANÇADO");
}
}
}];
}
我也尝试过使用dispatch_async(dispatch_get_main_queue(), ^{ [myDisplayedTable reloadData]; }); 重新加载数据,但效果不佳。
无论如何,我想如果我删除我的方法并将所有内容放入块中,它会起作用,但我不想这样做,因为使用 if 调用方法会使我的代码更容易遵循。
更新:
为了完整起见,这里是我的 DataSource 方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"numberOfRowsInSection %li", (unsigned long)_seriesForDisplay.count);
return _seriesForDisplay.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
PFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
PFObject *o = _seriesForDisplay[indexPath.row];
// Texto da célula
cell.textLabel.text = o[@"serieDisplay"];
cell.detailTextLabel.text = o[@"grupos"];
return cell;
}
【问题讨论】:
-
不是你的问题(还),但
[self popularSeriesInicianteC];永远不会被调用。 -
试试这个:stackoverflow.com/a/7914335/653513 应该可以工作,如果你没有你的
populate...方法也在后台工作。如果是这样,请在他们完成后致电reloadData... - 尽快。 -
我试过这个,但得到了崩溃消息:空数组的索引 0 超出范围'。我试图在块内使用此代码。当我记录块内的数组时,似乎这些方法正在正确填充数组。
-
您能否发布您的
populate...方法的代码。似乎他们也在单独的线程上工作......或者:你如何将数据传递给这些方法?他们是否有机会使用nil数据? -
这是什么
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object?dataSource委托方法的奇怪签名...简单尝试:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
标签: ios objective-c uitableview objective-c-blocks parse-platform