I have this error in xcode:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** Call stack at first throw:
I using this code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
int num_rows = 0;
switch (section) {
case 0:{
num_rows = [messageReceive count];
}
break;
case 1:{
num_rows = [messageSend count];
}
break;
return num_rows;
}
but i try many code but the same error. I using a UITableView to get message from a script php with Json. In “messageReceive” sometimes i have 0 or 1 or many messages. the same for messageSend.
thx
this is my cellForRowAtIndexPath
// Configure the cell.
NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row];
NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
switch ( indexPath.section )
{
case 0:
if (pseudoLabel.text =[dictMessSend objectForKey:@"sender"] )
{cell.detailTextLabel.text = [dictMessSend objectForKey:@"receiver"];
cell.text= [dictMessSend objectForKey:@"message"];
}
break;
case 1:
if (pseudoLabel.text =[dictMessReceive objectForKey:@"receiver"] )
{cell.detailTextLabel.text = [dictMessReceive objectForKey:@"sender"];
cell.text= [dictMessReceive objectForKey:@"message"];
}
else {
//cell.text = @"No message";
}
break;
}
return cell;
Where is the exception being raised? No where in the code you posted could that exception be raised unless you implemented a custom count method. So I will assume the exception is being raised in
tableView:cellForRowAtIndexPath:. Inside of that method (or any other similar methods) make sure you use the same logic.Next thing you want to do is make sure that messageReceive and messageSend are not mutable (your console output shows it was mutable). If they are properties then they should be an NSArray and set to copy not retain (ex.
@property(nonatomic, copy) NSArray *messageReceive;otherwise the code should look likemessageReceive = [sourceArray copy];Update:
The update you provide shows the problem is on the following line
You can not access both arrays with the same index, move that into the switch statement