请查看我的代码。应用程序工作正常,但当我快速向上或向下滚动它只是崩溃。
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引0超出范围 空数组'
这是cellforrawatindexpath方法
- (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;
}
实际上我正在从xml文件加载数据,该文件每隔5秒调用一次。我会在这里发布我的解析代码。
- (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];
});
});
}
这里是来自ViewDidLoad的NSTIMER
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target: self selector: @selector(Parse) userInfo: nil repeats: YES];
这是解析代码的其余部分
#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;
}
}
答案 0 :(得分:0)
看起来您的rssOutputData为空或低于numberOfRows。
请参阅使用rssOutputData,然后重置它:
// ...
previusCount = rssOutputData.count;
rssOutputData = [[NSMutableArray alloc]init];
// ...
在您分享的代码中,您不必再为其设置任何值。
答案 1 :(得分:0)
是的,要表明显而易见的,你试图访问一个不存在的数组中的元素。
问题:如果你"启用僵尸"在Xcode然后重新运行你的代码,它是否显示导致问题的那一行?
从那里,你应该能够找到原因。
您也可以尝试在cellForRowAtIndexPath
功能的顶部粘贴这样的内容......
if (rssOutputData == nil)
return;
if (rssOutputData.count <= objectAtIndex:indexPath.row)
return;
...但我仍然认为这个问题是由UITableView
试图(快速)加载和显示数据造成的,你的5秒函数正在忙着清空然后刷新。
毕竟,每5秒功能正在清空您的阵列,然后等待使用Web服务重新填充它。
在该网络服务完成之前,您的UITableView
绑定到一个没有数据的数据源......这可能不健康!!
rssOutputData = [[NSMutableArray alloc]init];
NSString *post =[[NSString alloc] initWithFormat:@"https://messages_%@.xml",
[[NSUserDefaults standardUserDefaults] stringForKey:@"xmls_id"]];
... load data from this web service...
难怪当你滚动时它会崩溃....
顺便说一句,在您的Parse
功能中,您将rssOutputData
值设置为您刚从网络服务中加载的内容...?