Ios 当我返回时,TableView显示相同数据两次或更多次?
当我从第二个tableViewController返回到第一个,从第一个viewController返回到第二个tableViewController时,xml文件数据重新加载到tableView上。当tableViewController中的数据首次出现时,它会显示两次相同的xml文件数据。为什么?我在我的项目中使用本地xml文件 //在第一视图控件中Ios 当我返回时,TableView显示相同数据两次或更多次?,ios,objective-c,xml,uitableview,xml-parsing,Ios,Objective C,Xml,Uitableview,Xml Parsing,当我从第二个tableViewController返回到第一个,从第一个viewController返回到第二个tableViewController时,xml文件数据重新加载到tableView上。当tableViewController中的数据首次出现时,它会显示两次相同的xml文件数据。为什么?我在我的项目中使用本地xml文件 //在第一视图控件中 - (void)viewDidLoad { [super viewDidLoad]; self.eastIndiaAr
- (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];
}
}
}
-(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;
}
//在tableViewControl中
- (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];
}
}
}
-(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;
}
这是因为每次在同一个旧数组中添加新对象时,请尝试删除数组中的旧对象。(在代码中需要的任何位置)
这是因为每次在同一个旧数组中添加新对象时,请尝试删除数组中的旧对象。(在代码中需要的任何位置) 喜欢
- (IBAction)onTapParseData:(UIButton *)sender {
// before call the request remove the previous value in your array
[self.eastIndiaArr removeAllObjects];
[self.westInadiaArr removeAllObjects];
[self.northIndiaArr removeAllObjects];
[self.southIndiaArr removeAllObjects];
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"india" ofType:@"xml"];
self.xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL fileURLWithPath:xmlFilePath]];
self.xmlParser.delegate = self;
[self.xmlParser parse];
}
喜欢
- (IBAction)onTapParseData:(UIButton *)sender {
// before call the request remove the previous value in your array
[self.eastIndiaArr removeAllObjects];
[self.westInadiaArr removeAllObjects];
[self.northIndiaArr removeAllObjects];
[self.southIndiaArr removeAllObjects];
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"india" ofType:@"xml"];
self.xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL fileURLWithPath:xmlFilePath]];
self.xmlParser.delegate = self;
[self.xmlParser parse];
}
没有这个
[self.tableView重载数据]
try onceAlready我尝试了这个,但它第二次也显示相同的数据…您期望的输出是什么我想在我再次返回时只在tableView中打印一次数据…好的,在您的页面中,您调用这个NSString*xmlFilePath=[[NSBundle mainbndle]pathForResource:@“india”of type:@“xml”]代码>不包含此[self.tableView重载数据]
try onceAlready我尝试了这个,但它第二次也显示相同的数据…您期望的输出是什么我想在我再次返回时只在tableView中打印一次数据…好的,在您的页面中,您调用这个NSString*xmlFilePath=[[NSBundle mainbndle]pathForResource:@“india”of type:@“xml”]
再次尝试重新加载表视图。@PratikShah-它是委托ifoundCharacters
的每次调用,因此在webservice调用之前删除其中的数据,它不是JSON,而是XMLtry再次加载表视图。@PratikShah-它是委托ifoundCharacters
的每次调用,所以在webservice调用之前删除数据,它不是JSON,而是XML