' NSRangeException',原因:' *** - [__ NSArrayM objectAtIndex:]:索引1超出界限[0 .. 0]'

时间:2016-04-26 05:42:31

标签: ios quickblox

在我的应用程序中,当我向其他用户发送附件(如图像和视频)时,这个错误随机出现,它会崩溃。

我正在使用 Quickblox SDK。

这是错误..

  

NSRangeException',原因:' *** - [__ NSArrayM objectAtIndex:]:索引1   超出界限[0 .. 0]

        2016-04-26 10:22:50.510 Sample-VideoChat[1027:12093] *** Terminating  app due to uncaught exception 'NSRangeException', reason:     '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

  enter code here
  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
 {
     NSLog(@"self.chatSections....%@",self.chatSections);
     return [self.chatSections count];
 }


 - (UICollectionViewCell *)collectionView:(QMChatCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
   {

    QBChatMessage *messageItem = [self messageForIndexPath:indexPath];

      NSRange textRange =[messageItem.text rangeOfString:@"https://api.quickblox.com/blobs/"];

     Class class = [self viewClassForItem:messageItem];
    NSString *itemIdentifier = [class cellReuseIdentifier];

     QMChatCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath];
cell.avatarView.tag = 0;

  if ((messageItem.attachments.count != 0) && textRange.location != NSNotFound) 
{

       QBChatAttachment* attachment = [[QBChatAttachment alloc] init];
         attachment.ID = @"3565772";
         attachment.url = messageItem.text;
         attachment.type = @"image";

         [cell.avatarView setImageWithURL:[NSURL URLWithString:attachment.url] placeholder:nil options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
     } completedBlock:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
     {
         cell.avatarView.tag = 4592;
     }];
}

cell.transform = self.collectionView.transform;

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;

[self collectionView:collectionView configureCell:cell forIndexPath:indexPath];

return cell;
   }

 - (QBChatMessage *)messageForIndexPath:(NSIndexPath *)indexPath {
         if (indexPath.item == NSNotFound) {
          // If the update item's index path has an "item" value of NSNotFound, it means it was a section update, not an individual item.
        return nil;
     }

      QMChatSection *currentSection = self.chatSections[indexPath.section];
     return currentSection.messages[indexPath.item];
     }

4 个答案:

答案 0 :(得分:16)

尝试添加一个异常断点(Add Exception Breakpoint)来调试异常发生的位置。它确实意味着您正在尝试访问不在数组索引中的对象。

最好找到导致问题的原因是在应用崩溃之前引发所有异常。

enter image description here

答案 1 :(得分:0)

试试这个:

if (item != nil && item.count != 0) {
    QMChatSection *currentSection = self.chatSections[indexPath.section];
    return currentSection.messages[indexPath.item];
}

答案 2 :(得分:0)

您可以像这样进行检查,在尝试访问索引之前检查值

com.somethingelse

如果您使用Quickblox创建自己的代码,可以参考他们的演示实现,因为它正常工作

答案 3 :(得分:0)

静态单元格 我遇到过同样的问题。所以,我只是替换了tableView内容类型

从“静态单元”到“动态原型”。

Image