提问人:Marking 提问时间:3/3/2017 更新时间:3/3/2017 访问量:80
当我回来时,TableView 显示两次或更多次相同的数据?
TableView displayed the same data two or more times when i get back?
问:
当我从第二个 tableViewController 返回到第一个和第一个 viewController 返回到第二个 tableViewController 时,我的 xml 文件数据重新加载到 tableView。第一次没有出现来自 tableViewController 的数据,它显示两次相同的 xml 文件数据。为什么?我在项目中使用本地 xml 文件。
在第一个视图中控制
- (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;
}
答:
3赞
Pratik Shah
3/3/2017
#1
这是因为每次在同一个旧 Array 中添加新对象时,请尝试删除数组的旧对象。(代码中任何需要的地方)
- (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];
}
评论
0赞
Pratik Shah
3/3/2017
再次尝试重新加载表视图。
1赞
Anbu.Karthik
3/3/2017
@PratikShah - 它是每次调用的委托,因此在 Web 服务调用之前删除数据,它不是 JSON,而是 XMLfoundCharacters
2赞
Anbu.Karthik
3/3/2017
#2
做赞
- (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 reloadData];
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"india" ofType:@"xml"];