我有一个表视图,其中包含使用 NSXML 解析器解析的 xml 链接列表,然后将解析结果推送到新视图。但是,它的行为非常奇怪。解析操作发生在用户选择单元格时,然后使用 NSXML 委托方法 didEndDocument,它通知带有 tableview 的对象继续并推送由解析结果填充的新视图控制器。在推送到新视图之前,我使用结果检查数组以确保它不为空。如果是,我不会推送新视图而只显示警报视图。每当我选择一个我知道将返回填充数组的单元格时,它都会认为该数组是空的并且不会推送视图。但随后我再次选择单元格并将其推送到新视图,显示正确的数据。它不会在解析操作完成之前过早地推送到新的视图控制器,除非过早调用 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];
}