【问题标题】:TableView displayed the same data two or more times when i get back?当我回来时,TableView 两次或多次显示相同的数据?
【发布时间】: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


【解决方案1】:

这是因为每次在同一个旧数组中添加新对象时。请尝试删除数组中的旧对象。(代码中需要的地方)

- (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];


}

【讨论】:

  • 尝试重新加载表格视图。
  • @PratikShah - 它是委托 ifoundCharacters 它每次调用,所以在 web 服务调用之前删除数据,它不是 JSON,它是 XML
【解决方案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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多