0

This is my code within tableView:cellForRowAtIndexPath: method,

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:@"name"];
cell.detailTextLabel.text = [[temp objectForKey:@"distance"] stringByAppendingString:@" km from Banglore"];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:@"category"]];
return cell;

I get exception,

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

Any pointers?

Venk
  • 5,949
  • 9
  • 41
  • 52
Vishal
  • 101
  • 1
  • 12

3 Answers3

1

Check your Tableview Delegate is like,

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.placeArray.count;
}

Otherwise let me know...

Venk
  • 5,949
  • 9
  • 41
  • 52
1

Exception itself provide you the answer. Your self.placeArray contains only 3 elements and you are trying to create more than 3 cells. You need to make sure that the self.placeArray's count was returning in numberOfRowsInSection method and also this array is not updating asynchronously while tableview is reloading.

HRM
  • 2,097
  • 6
  • 23
  • 37
1

The value you return in numberOfRowsForSection is greater than the number of objects in the placeArray.

after the line

NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];

add this

NSLog(@"number of places = %i", [placeArray count]);

Add a breakpoint at that line. You'll see that the app will crash when it hits this break point the fourth time, because numberOfRowsInSection is returning > 3 rows, and your placeArray has only <=3 objects

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52