2013-10-07 73 views
0

我想解析一個XML數據到表視圖,但數據沒有得到反映。我正在實現一個刷新按鈕,一旦點擊就從XML加載數據。有人可以告訴我我做錯了什麼嗎?以下是我的代碼XML沒有得到解析到tableview

// 
// 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 

回答

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