メディア事業部EC事業部 AGENCY事業部
メディア事業部ブログ
前のページへは、ブラウザの戻るボタンでお戻りください。
arefin

iPhone, XML Parse

In AreaViewController.h

#import
#import “UpdateViewController.h”

@interface AreaViewController : UITableViewController {

NSMutableArray *list;
NSString *area;

NSMutableArray *feedList;
NSMutableDictionary *currentItem;
NSMutableString *currentContents;

UpdateViewController *updateViewC;

}

@property (nonatomic, retain) UpdateViewController *updateViewC;

@end

:arrow: :arrow: :arrow:

In AreaViewController.m

#import “AreaViewController.h”
#import “UpdateViewController.h”

@implementation AreaViewController

@synthesize updateViewC;

- (void)viewWillAppear:(BOOL)animated{

// Update the view with current data before it is displayed.
[super viewWillAppear:animated];

feedList = [[NSMutableArray alloc] initWithCapacity:0];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/area.xml"];
NSData *xmldData = [fileManager contentsAtPath:fileName];

if (xmldData == nil) {
// make a alert
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@”Update” message:@”Please Update Your app.” delegate:self cancelButtonTitle:@”Cancle” otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:@"Update"];
[alert show];
}

NSXMLParser *nextParser = [[NSXMLParser alloc] initWithData:xmldData];

[nextParser setDelegate:self];
[nextParser parse];

// Scroll the table view to the top before it appears
[self.tableView reloadData];

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

if (buttonIndex == 1) {

// update all xml data from server
UpdateViewController *updateViewControl = [[UpdateViewController alloc]initWithNibName:@”UpdateView” bundle:[NSBundle mainBundle]];

self.updateViewC = updateViewControl ;
[updateViewControl release];

[self.navigationController pushViewController:self.updateViewC animated:YES];

}else {
// press entrt pop search scren
[self.navigationController popViewControllerAnimated:YES];
}

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

if ([elementName compare:@"item"] == NSOrderedSame) {
currentItem = [[NSMutableDictionary alloc] initWithCapacity:0];
} else if (currentItem != NULL) {
currentContents = [[NSMutableString alloc] initWithCapacity:0];
}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if (currentContents && string) {

[currentContents appendString:string];
}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName compare:@"item"] == NSOrderedSame) {

[feedList addObject:currentItem];
[currentItem release];
} else if (currentItem && currentContents) {
[currentItem setObject:currentContents forKey:elementName];
currentContents = nil;
[currentContents release];

}

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

[parser release];

}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [feedList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”BigCategory”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

//cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}

if ([[feedList objectAtIndex:indexPath.row] objectForKey:@”title”]) {
cell.textLabel.text = [[feedList objectAtIndex:indexPath.row] objectForKey:@”title”];
}

//if ([[feedList objectAtIndex:indexPath.row] objectForKey:@”id”]) {
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//}

return cell;

}

#pragma mark -
#pragma mark Table view selection

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/addressType.txt"];
NSString *areaString;
NSData *areaData;

switch (indexPath.row) {

case 0:

// write address type
areaString = [NSString stringWithFormat:@""];
areaData = [areaString dataUsingEncoding:NSUTF8StringEncoding];
[fileManager createFileAtPath:fileName contents:areaData attributes:nil];

//[self.navigationController popViewControllerAnimated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

break;

default:

// write address type
areaString = [[feedList objectAtIndex:indexPath.row] objectForKey:@”title”];
areaData = [areaString dataUsingEncoding:NSUTF8StringEncoding];
[fileManager createFileAtPath:fileName contents:areaData attributes:nil];

//[self.navigationController popViewControllerAnimated:YES];
//[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -3)] animated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

break;

}

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

// Cancle button and add an event handler
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@”Cancle”
style:UIBarButtonSystemItemCancel
target:self
action:@selector(handleCancle:)];
}

- (void) handleCancle:(id)sender
{
// pop the controller
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}

- (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.
}

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

- (void)dealloc {
[super dealloc];
[feedList release];
}

@end

カテゴリー:iPad, iPhone , 投稿者名:Arefin Khandoker
PAGE TOP
ページトップへ