我有一个tableview,其中包含一个xml链接列表,这些链接使用NSXML解析器进行解析,然后使用解析后的结果推送到新视图。然而,它表现得很奇怪。解析操作发生在用户选择单元格,然后使用NSXML委托方法didEndDocument时,它会通过tableview通知对象继续并推送由解析结果填充的新视图控制器。在推送到新视图之前,我检查数组中的结果以确保它不为空。如果是,我不会推送新视图,只显示警报视图。每当我选择一个我知道将返回一个填充数组的单元格时,它认为该数组是空的并且不会推送视图。但后来我再次选择了单元格并将其推送到新视图,显示正确的数据。除非在pre-maturely中调用didEndDocument委托方法,否则在解析操作完成之前不会过早地推送到新的视图控制器。任何帮助将不胜感激。
代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.center = selectedCell.accessoryView.center;
selectedCell.accessoryType = UITableViewCellAccessoryNone;
selectedCell.accessoryView = activity;
[activity startAnimating];
departmentName = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"name"];
[self performSelector:@selector(parse:) withObject:[[sortedArray objectAtIndex:indexPath.row] objectForKey:@"link"] afterDelay:0];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)parse:(NSString *)link {
NSLog(@"Link = %@", link);
kxmlParser = [[KMXMLParser alloc] loadXMLByURL:link delegate:self];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"Did Start");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"Did End");
NSLog(@"Array = %@", kxmlParser.posts);
[activity stopAnimating];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
MainViewController *main = [storyboard instantiateViewControllerWithIdentifier:@"main"];
[main setDepartmentname:departmentName];
[self.navigationController pushViewController:main animated:YES];
}