jjuser19jj
jjuser19jj

Reputation: 1699

PFQueryTableViewController sections

I have a PFQueryTableViewController and I want to add sections to the tableview, I try it like this:

- (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;
  }

But I always get this error:

Terminating app due to uncaught exception

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

I know what it means but don't see why. When I change rows = [self.objects count], for section = 0, it works.

Thanks for your help

EDIT: call stack: Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' * First throw call stack: ( 0 CoreFoundation 0x02b085e4 exceptionPreprocess + 180 1 libobjc.A.dylib 0x0288b8b6 objc_exception_throw + 44 2 CoreFoundation 0x02aa9556 -[__NSArrayM objectAtIndex:] + 246 3 Challenger 0x0003e573 -[PFQueryTableViewController objectAtIndexPath:] + 69 4 Challenger 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 Challenger 0x0000967d main + 141 26 libdyld.dylib 0x0481870d start + 1 27 ??? 0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

Upvotes: 0

Views: 638

Answers (1)

Brandon Schlenker
Brandon Schlenker

Reputation: 5088

You'll want to override - (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;
}

Upvotes: 1

Related Questions