PFQueryTableViewController部分

时间:2014-02-13 12:57:43

标签: ios uitableview parse-platform

我有一个PFQueryTableViewController,我想在tableview中添加部分,我试试这样:

- (PFQuery *)queryForTable {
  PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

  // If no objects are loaded in memory, we look to the cache first to fill the table
  // and then subsequently do a query against the network.
  if (self.objects.count == 0) {
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
  }

  [query orderByDescending:@"createdAt"];

  return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
                       object:(PFObject *)object {
  static NSString *cellIdentifier = @"Challenge";
  PFTableViewCell* cell;


  if (indexPath.section == 0)
  {
      cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      if (!cell)
      {
          cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
      }
      cell.textLabel.text = @"Test section 1";
  }

  if (indexPath.section == 1) 
  {
      cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      if (!cell)
      {
          cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
      }
      cell.textLabel.text = @"Test section 2";
  }

  return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      NSInteger rows = 0;

      if (section == 0)
      {
          rows = 1;
      }
      if (section == 1)
      {
          rows = [self.objects count];
      }

      return rows;
  }

但我总是得到这个错误:

由于未捕获的异常而终止应用

'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: 
index 0 beyond bounds for empty array'

我知道这意味着什么,但不明白为什么。当我更改rows = [self.objects count]时,对于section = 0,它可以正常工作。

感谢您的帮助

编辑:调用堆栈: 由于未捕获的异常'NSRangeException'而终止应用程序,原因:' * - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界' * 第一次抛出调用堆栈: (     0 CoreFoundation 0x02b085e4 exceptionPreprocess + 180     1 libobjc.A.dylib 0x0288b8b6 objc_exception_throw + 44     2 CoreFoundation 0x02aa9556 - [__ NSArrayM objectAtIndex:] + 246     3挑战者0x0003e573 - [PFQueryTableViewController objectAtIndexPath:] + 69     4挑战者0x0003e77e - [PFQueryTableViewController tableView:cellForRowAtIndexPath:] + 184     5 UIKit 0x016ecd2f - [UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412     6 UIKit 0x016ece03 - [UITableView _createPreparedCellForGlobalRow:] + 69     7 UIKit 0x016d1124 - [UITableView _updateVisibleCellsNow:] + 2378     8 UIKit 0x016e45a5 - [UITableView layoutSubviews] + 213     9 UIKit 0x01668dd7 - [UIView(CALayerDelegate)layoutSublayersOfLayer:] + 355     10 libobjc.A.dylib 0x0289d81f - [NSObject performSelector:withObject:] + 70     11 QuartzCore 0x00a3b72a - [CALayer layoutSublayers] + 148     12 QuartzCore 0x00a2f514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380     13 QuartzCore 0x00a2f380 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26     14 QuartzCore 0x00997156 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294     15 QuartzCore 0x009984e1 _ZN2CA11Transaction6commitEv + 393     16 QuartzCore 0x00998bb4 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92     17 CoreFoundation 0x02ad053e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 30     18 CoreFoundation 0x02ad048f __CFRunLoopDoObservers + 399     19 CoreFoundation 0x02aae3b4 __CFRunLoopRun + 1076     20 CoreFoundation 0x02aadb33 CFRunLoopRunSpecific + 467     21 CoreFoundation 0x02aad94b CFRunLoopRunInMode + 123     22 GraphicsServices 0x0356e9d7 GSEventRunModal + 192     23 GraphicsServices 0x0356e7fe GSEventRun + 104     24 UIKit 0x015fe94b UIApplicationMain + 1225     25挑战者0x0000967d主+ 141     26 libdyld.dylib 0x0481870d start + 1     27 ??? 0x00000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止

1 个答案:

答案 0 :(得分:1)

您需要覆盖- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath

- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
{
    PFObject *obj = nil;
    if (indexPath.row < self.objects.count)
    {
        obj = [self.objects objectAtIndex:indexPath.row];
    }

    return obj;
}