0

我正在尝试将 XML 数据解析到表视图中,但数据没有得到反映。我正在实现一个刷新按钮,一旦单击该按钮就会从 XML 加载数据。有人可以告诉我我做错了什么吗?下面是我的代码

//
//  Tab1.m
//  Sample
//
//  Created by Vishal Thakur on 10/3/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "Tab1.h"
#import "TAB2.h"
#import "Fruit.h"

@interface Tab1() {
 TAB2 *t2;
 Fruit *aFruit;
 NSString *mark;
}
@property (retain, nonatomic) NSArray *fruits;
@end

@implementation Tab1
@synthesize table;
@synthesize fruits;
@synthesize fruitsDictionary;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.fruits = [NSArray arrayWithObjects:@"Apple", @"PineApple", @"Orange", @"Banana", @"Pear", @"Kiwi", @"Strawberry", nil];
         NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Names" ofType:@"xml"];

         NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];

         [parser setDelegate:self];
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 44.0)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(50, 10, 80, 40)];
   [button setTitle:@"Refresh" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(RefreshTable:) forControlEvents:UIControlEventTouchUpInside];    [customView addSubview:button];

    self.table.tableHeaderView = customView;

    }
    return self;
}

-(void)RefreshTable:(id)sender {


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Names" ofType:@"xml"];

     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];

   [parser setDelegate:self];

    if ([parser parse]) {
            NSLog(@"successful");
      NSLog(@"::::%@",fruitsDictionary);
      ///////DetailScreen *ds=[[DetailScreen alloc]init];

       [self.table reloadData];
    }
   else {
       NSLog(@"Failure!");
   }

   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    NSInteger count= [self.fruits count];
    return count; 
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSArray *keys = [fruitsDictionary allKeys];
   NSDictionary *local = [NSDictionary dictionaryWithObject:[fruitsDictionary valueForKey:[keys objectAtIndex:indexPath.row]]forKey:[keys objectAtIndex:indexPath.row]];

    NSLog(@"%@",keys);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fruit" message:(NSString *)[keys objectAtIndex:indexPath.row] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
    [alert show];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell Identifier";
 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    }
    NSArray *keys = [fruitsDictionary allKeys];
    NSString *fruit = [keys objectAtIndex:[indexPath row]];
    [cell.textLabel setText:fruit];

    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"Fruit"]) {
        aFruit = [[Fruit alloc] init];
        if (!fruitsDictionary) {
            fruitsDictionary = [[NSMutableDictionary alloc] init];
        }
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    currentElement = nil;
    currentElement = [[NSMutableString alloc] initWithString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:@"Name"]) {
        aFruit.name = currentElement;
    }
    else if ([elementName isEqualToString:@"Description"]) {
        aFruit.description = currentElement;
    }
    else if ([elementName isEqualToString:@"Fruit"]) {
        [fruitsDictionary setObject:aFruit forKey:aFruit.name];
    }
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [self setTable:nil];
    [self setFruits:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate


@end
4

1 回答 1

0

您可以使用该parserDidStartDocument:方法来执行此操作。

根据NSXMLParserDelegate

parserDidEndDocument

解析器对象在成功完成解析后发送给委托。

- (void)parserDidEndDocument:(NSXMLParser *)parser

参数

解析器

A parser object.

可用性

Available in iOS 2.0 and later.
Available as part of an informal protocol prior to iOS 4.0.

也可以看看

– parserDidStartDocument:

在 NSXMLParser.h 中声明

您可以像这样实现它:

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
   [self.table reloadData];
}
于 2013-10-08T10:00:53.763 回答