2011-06-22 62 views
1

我有这样的错误在Xcode:的tableView numberOfRowsInSection

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]' 
*** Call stack at first throw: 

我使用此代码:

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

,但我尝试了很多代码,但同样的错误。我使用UITableView从JSON脚本php获取消息。在“messageReceive”中,有时我有0或1个或许多消息。 messageSend也一样。

THX

这是我的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; 
+0

发布cellForRowAtIndexPath函数的代码。异常很可能来自那里。 –

+0

那么你的'的tableView:的cellForRowAtIndexPath:'方法看起来像 – Intentss

+0

看到我的编辑THX – XcodeMania

回答

2

在哪里提出异常?除非您实施自定义计数方法,否则不会在您发布的代码中的哪个位置引发该异常。所以我会假设在tableView:cellForRowAtIndexPath:中提出例外。在该方法(或任何其他类似的方法)内部确保使用相同的逻辑。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch (indexPath.section) { 
     case 0:{ 
      //messageReceive logic 
     } 
     break; 
     case 1:{ 
      //messageSend logic 
     } 
     break; 
    } 
} 

你想要做的下一件事是确保messageReceive和messageSend是不可变的(控制台输出显示它是可变的)。如果他们的属性,他们应该是一个NSArray和设置复制不保留(例如@property(nonatomic, copy) NSArray *messageReceive;否则代码看起来应该像messageReceive = [sourceArray copy];

更新:

您提供的更新显示的问题是以下线

NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row]; 
NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row]; 

你不能用相同的索引访问两个数组,将其写入switch语句

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; 
} 
+0

看看我的编辑THX – XcodeMania

+0

一个好的标题我我的答案更新 – Joe

+0

是的,它是很好的THX乔:) – XcodeMania

0

发表您的cellForRowAtIndexPath方法,那你要去哪里错了,你可能使用了错误的数组的某些部分......因此,对于部分0您使用收到的消息(可能是5),并在cellForRowAtIndexPath您正在阅读的消息发送数组是小于5,所以你得到的界外例外..

相关问题