【发布时间】:2017-07-23 02:02:40
【问题描述】:
当我从第二个 tableViewController 返回到第一个和第一个 viewController 到第二个 tableViewController 时,我的 xml 文件数据重新加载到 tableView。来自 tableViewController 的第一次数据不会出现,它显示两次相同的 xml 文件数据。为什么?我在我的项目中使用本地 xml 文件。
//在第一个viewControl中
- (void)viewDidLoad {
[super viewDidLoad];
self.eastIndiaArr = [[NSMutableArray alloc]init];
self.westInadiaArr = [[NSMutableArray alloc]init];
self.southIndiaArr = [[NSMutableArray alloc]init];
self.northIndiaArr = [[NSMutableArray alloc]init];
}
- (IBAction)onTapParseData:(UIButton *)sender {
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"india" ofType:@"xml"];
self.xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL fileURLWithPath:xmlFilePath]];
self.xmlParser.delegate = self;
BOOL success = [self.xmlParser parse];
if (success) {
NSLog(@"Parse success");
} else {
NSLog(@"Parse not success");
}
StatesTableViewController *stvc = [self.storyboard instantiateViewControllerWithIdentifier:@"STVC"];
stvc.eastIndiaArr1 = self.eastIndiaArr;
stvc.westIndiaArr1 = self.westInadiaArr;
stvc.northIndiaArr1 = self.northIndiaArr;
stvc.southIndiaArr1 = self.southIndiaArr;
[self.navigationController pushViewController:stvc animated:YES];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trimmedString.length > 0) {
if ([self.zoneNodeElement isEqualToString:@"eastIndia"]) {
[self.eastIndiaArr addObject:string];
} else if ([self.zoneNodeElement isEqualToString:@"westIndia"]) {
[self.westInadiaArr addObject:string];
} else if ([self.zoneNodeElement isEqualToString:@"northIndia"]) {
[self.northIndiaArr addObject:string];
} else if ([self.zoneNodeElement isEqualToString:@"southIndia"]) {
[self.southIndiaArr addObject:string];
}
}
}
//在tableViewControl中
-(void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];
if (indexPath.section == 0) {
cell.textLabel.text = [self.eastIndiaArr1 objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.textLabel.text = [self.westIndiaArr1 objectAtIndex:indexPath.row];
} else if (indexPath.section == 2) {
cell.textLabel.text = [self.northIndiaArr1 objectAtIndex:indexPath.row];
} else if (indexPath.section == 3) {
cell.textLabel.text = [self.southIndiaArr1 objectAtIndex:indexPath.row];
}
return cell;
}
【问题讨论】:
-
没有这个
[self.tableView reloadData];尝试一次 -
我已经试过这个了,但它第二次显示相同的数据......
-
你期望的输出
-
当我再回来一次时,我只想在 tableView 中打印一次数据....
-
在你的页面中确定你调用了多少次
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"india" ofType:@"xml"];
标签: ios objective-c xml uitableview xml-parsing