please view my code. Application is working ok but when i scroll fast of up or down it just crashes.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Here is cellforrawatindexpath method
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"];
if (cell == nil) {
NSArray *nib;
if ([[[rssOutputData objectAtIndex:indexPath.row] xml_userType] isEqualToString:@"2"])// THIS IS THE LINE WHERE IT CRASHES
{
nib = [[NSBundle mainBundle] loadNibNamed:@"OutGoing" owner:self options:nil];
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(58,12,241,30 )];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
}else {
nib = [[NSBundle mainBundle] loadNibNamed:@"InComing" owner:self options:nil];
textLabel =[[UILabel alloc] initWithFrame:CGRectMake(21,12,241,30)];
cell.textLabel.textAlignment = NSTextAlignmentRight;
}
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
textLabel = (UILabel *)[cell viewWithTag:1];
userLabel = (UILabel *)[cell viewWithTag:2];
avatar = (UIImageView *)[cell viewWithTag:3];
timeLabel = (UILabel *)[cell viewWithTag:4];
NSString * test=[[rssOutputData objectAtIndex:indexPath.row]xml_msg];
textLabel.text = test;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
[cell addSubview:textLabel];
userLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xml_username];
avatar.layer.cornerRadius = 20.0;
avatar.layer.masksToBounds = YES;
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"patient_name"] isEqualToString:[[rssOutputData objectAtIndex:indexPath.row]xml_username]] || [[[NSUserDefaults standardUserDefaults] stringForKey:@"therapist"] isEqualToString:[[rssOutputData objectAtIndex:indexPath.row]xml_username]]) {
avatar.image = [UIImage imageNamed:@"p.jpg"];
}else {
avatar.image = [UIImage imageNamed:@"t.jpg"];
}
rectTime = (CGRect){0, textLabel.frame.size.height+5, 241, 11};
timeLabel = [[UILabel alloc] initWithFrame:rectTime];
timeLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
timeLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xml_addedTime];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.textColor = [UIColor lightGrayColor];
[textLabel addSubview:timeLabel];
return cell;
}
actually i am loading data from xml file which is calling after every 5 sec. i will post my parsing code here.
- (void) Parse{
previusCount = rssOutputData.count;
rssOutputData = [[NSMutableArray alloc]init];
NSString *post =[[NSString alloc] initWithFormat:@"https://messages_%@.xml",[[NSUserDefaults standardUserDefaults] stringForKey:@"xmls_id"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0),^{
NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:post]];
xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
[xmlParserObject setDelegate:self];
[xmlParserObject parse];
dispatch_async(dispatch_get_main_queue(), ^{
[messageList reloadData];
});
});
}
and here is the NSTIMER from ViewDidLoad
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target: self selector: @selector(Parse) userInfo: nil repeats: YES];
here is rest of the parsing code
#pragma mark NSXMLParser delegate
//below delegate method is sent by a parser object to provide its delegate when it encounters a start tag
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"message"]){
xmlStringFileObject =[[XMLChatFile alloc]init];
} else {
nodecontent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"message"]){
[rssOutputData addObject:xmlStringFileObject];
xmlStringFileObject = nil;
}
else if([elementName isEqualToString:@"added"]){
xmlStringFileObject.xml_addedTime= nodecontent;
nodecontent = nil;
}
else if([elementName isEqualToString:@"user"]){
xmlStringFileObject.xml_username= nodecontent;
nodecontent = nil;
}
else if([elementName isEqualToString:@"text"]){
xmlStringFileObject.xml_msg= nodecontent;
nodecontent = nil;
}
else if([elementName isEqualToString:@"user_type"]){
xmlStringFileObject.xml_userType= nodecontent;
nodecontent = nil;
}
}