2011-06-22 61 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,所以你得到的界外例外..

相關問題