tableView numberOfRowsInSection
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.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:{
//messageReceive logic
}
break;
case 1:{
//messageSend logic
}
break;
}
}
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 like messageReceive = [sourceArray copy];
Update:
The update you provide shows the problem is on the following line
NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row];
NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
You can not access both arrays with the same index, move that into the switch statement
NSDictionary * dictMessReceive = nil;
NSDictionary * dictMessSend = nil;
switch ( indexPath.section )
{
case 0:
dictMessageReceive = [self.messageReceive objectAtIndex:indexPath.row];
//...
break;
case 1:
dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
//...
break;
}
post your cellForRowAtIndexPath method, thats where you are going wrong, you are probably using the wrong array for certain sections...So for section 0 you use messages received (which might be 5) and in cellForRowAtIndexPath you are reading from the messages sent array which is less that 5, and so you get the out of bounds exception..
精彩评论