我回来时,TableView显示相同的数据两次或更多次?

时间:2017-03-03 04:39:36

标签: ios objective-c xml uitableview xml-parsing

当我从第二个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;
}

2 个答案:

答案 0 :(得分:3)

这是因为每次在同一个旧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];


}

答案 1 :(得分: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];
}