2014-10-19 49 views
1

方案解析iOS版SDK:基于结果的查询

我有一个应用程序的PFQueryTableViewController动态单元格高度,从我的parse.com数据库并显示到PFQueryTableViewController下载数据。 PFQTVC有多个(6)原型单元,每个原型单元都有自己的重用标识符以及最重要的是它们自己的高度。我目前使用下面的设计查找每个indexPath.row正确的高度...

目前

使寻呼

每个单元在此之前的工作是为每一个显示不同类型的独特的信息。所以我正在检查每个self.objects他们每个独特的质量。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

         // \!/ -trouble maker 
    PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
    NSString *orientation = object[@"orientation"]; 

    NSNumber *height; 

    if ([orientation isEqual: @"left"] || [orientation isEqual: @"right"]) { 

     height = @400.0; 
    } 

    else if ([orientation isEqual: @"up"] || [orientation isEqual: @"down"]) { 

     height = @500.0; 
    } 

    else if (blah blah blah) { 

     //ect. 
    } 

    return [height floatValue]; 
} 

这一切都有效。也就是说,在启用分页之前。

当我启用分页

像我需要通过数据能够页面大多数应用程序,这是我做什么。我把我的objectsPerPage 10 - 问题是,当我向下滚动到第十细胞并重新加载下一页......

...reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]' 

原因

当它加载下一页,self.objects in heightForRowAtIndexPath是一个NSArray,它不会被新对象加载。这导致我的应用程序崩溃,让我非常沮丧。

问题

有谁知道如何解决这个问题?

编辑

NSNumber *height; 

if (indexPath.row > self.objects.count) { 

    height = @50.0; 
} 

else { 

    PFObject *object = [self.objects objectAtIndex:indexPath.row]; 

    NSString *orientation = object[@"orientation"]; 

    if ([orientation isEqual: @"left"] || [orientation isEqual: @"right"]) { 
     //HAS COMMENTS 
     if ([[object objectForKey:@"comments"]intValue] > 0) { 


      height = @618.0; 

     } 

     else { 


      //NO COMMENTS BUT HAS LIKES 
      if ([[object objectForKey:@"likes"]count] > 0) { 


       height = @398.0; 

      } 
      //HAS NOTHING 
      else { 


       height = @381.0; 

      } 

     } 

    } 

    else if ([orientation isEqual: @"up"] || [orientation isEqual: @"down"]) { 

     if ([[object objectForKey:@"comments"]intValue] > 0) { 


      height = @727.0; 

     } 

     else { 


      if ([[object objectForKey:@"likes"]count] > 0) { 


       height = @508.0; 

      } 

      else { 

       height = @490.0; 

      } 

     } 

    } 

    return [height floatValue]; 

} 

回答

1

启用了分页时,解析补充是一笔11单元的“加载更多”细胞。尽管如此,self.objects中没有第11个对象。您需要检查indexPath.row < self.objects.count并为“加载更多”单元格返回不同的高度。

这应该防止错误:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // \!/ -trouble maker 
    if (indexPath.row== self.objects.count) return 40.0; 

    PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
    NSString *orientation = object[@"orientation"]; 

    NSNumber *height; 

    if ([orientation isEqual: @"left"] || [orientation isEqual: @"right"]) { 

     height = @400.0; 
    } 

    else if ([orientation isEqual: @"up"] || [orientation isEqual: @"down"]) { 

     height = @500.0; 
    } 

    else if (blah blah blah) { 

     //ect. 
    } 

    return [height floatValue]; 
} 

当我实现了loadNextPage稍有延迟它的伟大工程。必须有一个方法在正确加载之前尝试访问self.objects数组。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row >= self.objects.count) 
    { 
     // this method gets called when the cell is scrolling into view, but also when it's first added to the table view 
     // we only care about the first case 
     if ([tableView.indexPathsForVisibleRows containsObject:indexPath]) 
     { 
      [self performSelector:@selector(loadNextPage) withObject:nil afterDelay:.2]; 
     } 
    } 
} 
+0

也许我没有解释清楚道歉。我也使用这个方便的方法。这很重要吗? [方法](https://www.parse.com/questions/pfqeurytableviewcontroller-load-next-page-automatically)。我想,因为我有这个我不需要一个加载更多的单元格。 – 2014-10-20 01:32:38

+0

在willDisplayCell被调用之前,它很可能会调用heightForRow方法。即使它没有下一页可能异步加载,所以仍然有可能获得“加载更多”单元格。 – hybrdthry911 2014-10-20 01:37:55

+0

我尝试了你的建议,并在上面发布了我的代码,但它仍然表示NSArrayM 0..10出界和全部。 – 2014-10-20 01:59:21