当我从第二个 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;
}
最佳答案
这是因为每次在同一个旧数组中添加新对象时。尝试删除数组中的旧对象。(代码中需要的任何地方)
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trimmedString.length > 0) {
// remove the old object
[self.eastIndiaArr removeAllObjects];
[self.westInadiaArr removeAllObjects];
[self.northIndiaArr removeAllObjects];
[self.southIndiaArr removeAllObjects];
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];
}
}
// reload the tableview
[self.tableView reloadData];
}
关于ios - 当我回来时,TableView 显示相同的数据两次或更多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570775/