我正在做一个聊天应用程序并从firebase中恢复数据。但在聊了一段时间之后就崩溃了。我无法找到问题在哪个阵列中。请帮助我找出哪个阵列。我正在使用Jsqmessageviewcontroller库。
NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 21 beyond bounds [0 .. 19]' *** First throw call stack: ( 0 CoreFoundation 0x03bbc946 __exceptionPreprocess + 182 1 libobjc.A.dylib 0x02d33a97 objc_exception_throw + 44 2 CoreFoundation 0x03a92a93 -[__NSArrayM objectAtIndex:] + 243 3 UIKit 0x022f0a67 -[_UIFlowLayoutSection frameForItemAtIndexPath:] + 142 4 UIKit 0x022b5273 -[UICollectionViewFlowLayout _frameForItem:inSection:usingData:] + 123 5 UIKit 0x022b0e07 -[UICollectionViewFlowLayout layoutAttributesForItemAtIndexPath:usingData:] + 510 6 UIKit 0x022b1037 -[UICollectionViewFlowLayout layoutAttributesForItemAtIndexPath:] + 339 7 ChatApp 0x001af154 -[JSQMessagesCollectionViewFlowLayout layoutAttributesForItemAtIndexPath:] + 100 8 ChatApp 0x001b05ab -[JSQMessagesCollectionViewFlowLayout sizeForItemAtIndexPath:] + 123 9 ChatApp 0x001c53a2 -[JSQMessagesViewController scrollToBottomAnimated:] + 1266 10 ChatApp 0x000a2fe7 __37-[ChatpageViewController viewDidLoad]_block_invoke_3 + 103 11 libdispatch.dylib 0x045ff30a _dispatch_call_block_and_release + 15 12 libdispatch.dylib 0x0461fe2f _dispatch_client_callout + 14 13 libdispatch.dylib 0x0460690e _dispatch_main_queue_callback_4CF + 606 14 CoreFoundation 0x03b1695e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 15 CoreFoundation 0x03ad5760 __CFRunLoopRun + 2256 16 CoreFoundation 0x03ad4bcb CFRunLoopRunSpecific + 443 17 CoreFoundation 0x03ad49fb CFRunLoopRunInMode + 123 18 GraphicsServices 0x04ed124f GSEventRunModal + 192 19 GraphicsServices 0x04ed108c GSEventRun + 104 20 UIKit 0x01b6a8b6 UIApplicationMain + 1526 21 ChatApp 0x000e020d main + 141 22 libdyld.dylib 0x0464bac9 start + 1 23 ??? 0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
- (id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString * chat = [[DataArray objectAtIndex:indexPath.row] objectForKey:@"Message"];
NSString * imageurl = [[DataArray objectAtIndex:indexPath.row] objectForKey:@"media"];
NSString * vidimage = [[DataArray objectAtIndex:indexPath.row] objectForKey:@"imgurl"];
if (chat == nil)
{
if ([imageurl rangeOfString:@".jpg"].location == NSNotFound)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filepathurl = [NSURL URLWithString:imageurl];
NSString *filewithext = [[filepathurl path] lastPathComponent];
NSString *combined = [NSString stringWithFormat:@"%@/%@", documentsDirectory, filewithext];
NSLog(@"=====>%@",combined);
NSLog(@"Video is Loading....");
[image1 sd_setImageWithURL:[NSURL URLWithString:vidimage]placeholderImage:nil];
vitem = [[JSQPhotoMediaItem alloc] initWithImage:image1.image];
// NSString * date12=[[DataArray objectAtIndex:indexPath.row] objectForKey:@"date"];
if(![self.senderId isEqualToString:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"UserId"]]){
vitem .appliesMediaViewMaskAsOutgoing = NO;
}
NSString *dateString = [[DataArray objectAtIndex:indexPath.row] objectForKey:@"date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-YY HH:mm a"];
dateFromString = [[NSDate alloc] init];
dateFromString=[dateFormatter dateFromString:dateString];
messageForRow = [[JSQMessage alloc] initWithSenderId:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"UserId"] senderDisplayName:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"Name"]date:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"date"] media:vitem] ;
// JSQPhotoMediaItem *vitem = [[JSQPhotoMediaItem alloc] initWithImage:thumbnailImage];
}
else
{
image1 =[[UIImageView alloc]init];
[image1 sd_setImageWithURL:[NSURL URLWithString:imageurl]
placeholderImage:nil];
JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:image1.image];
if(![self.senderId isEqualToString:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"UserId"]]){
item .appliesMediaViewMaskAsOutgoing = NO;
}
NSString *dateString = [[DataArray objectAtIndex:indexPath.row] objectForKey:@"date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-YY HH:mm a"];
//dateFromString = [[NSDate alloc] init];
dateFromString=[dateFormatter dateFromString:dateString];
messageForRow = [[JSQMessage alloc] initWithSenderId:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"UserId"] senderDisplayName:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"Name"] date:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"date"] media:item] ;
}
}
else
{
messageForRow = [[JSQMessage alloc] initWithSenderId:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"UserId"] senderDisplayName:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"Name"] date:[[DataArray objectAtIndex:indexPath.row] objectForKey:@"date"] text: [[DataArray objectAtIndex:indexPath.row] objectForKey:@"Message"]] ;
}
return messageForRow;
}