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?

有帮助吗?

解决方案

Check your Tableview Delegate is like,

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

    return self.placeArray.count;
}

Otherwise let me know...

其他提示

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.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top