1.主檔案: RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSMutableArray *rssList;
NSMutableDictionary *currentItem;
NSMutableString *currentContents;
}
@property (nonatomic, retain) NSMutableArray *rssList;
@end
#import "RootViewController.h"
#import "InternetNewsAppDelegate.h"
#import "DetailViewController.h"
@implementation RootViewController
@synthesize rssList;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = @"國際要聞";
self.rssList = [[NSMutableArray alloc] init];
//NSXMLParser *firstParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://rss.sina.com.cn/news/world/focus15.xml"]];
NSXMLParser *firstParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.alibuybuy.com/feed"]];
firstParser.delegate = self;
[firstParser parse];
}
#pragma mark -
#pragma mark NSXMLParser delegate Methods
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(@"startParser----------------------");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"end:----------------------");
[parser release];
NSLog(@"%@",rssList);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"item"])
{
currentItem = [[NSMutableDictionary alloc] init];
}
else if (currentItem != NULL)
{
currentContents = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"item"])
{
[rssList addObject:currentItem];
[currentItem release];
}
else if (currentContents && currentItem)
{
[currentItem setObject:currentContents forKey:elementName];
[currentContents release];
currentContents = nil;
}
}
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
if(currentItem && currentContents)
{
currentContents = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(currentItem && currentContents)
{
[currentContents appendString:string];
}
}
/*
- (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];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rssList count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [[rssList objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
/*
// 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 -
#pragma mark Table view delegate
//當點選cell後利用 NSNotificationCenter将消息傳遞給 DetailViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *des = [[rssList objectAtIndex:indexPath.row]objectForKey:@"description"];
DetailViewController *detailView = [[DetailViewController alloc] init];
NSLog(@"%@", self.navigationController.view);
[[self navigationController] pushViewController:detailView animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:@"onClicked" object:des];
[detailView release];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[rssList release];
[super dealloc];
}
@end
DetailViewController (詳細展示頁)
//
// DetailViewController.m
// InternetNews
//
// Created by zhang peng on 13-4-16.
//
//
#import "DetailViewController.h"
#import "RootViewController.h"
#import "InternetNewsAppDelegate.h"
@implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 800)];
label.numberOfLines = 10;
InternetNewsAppDelegate *del = [(InternetNewsAppDelegate *)[UIApplication sharedApplication] delegate];
NSLog(@"%@", self.view);
[self.view addSubview:label];
//label.text = del.des;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"onClicked" object:nil];
}
return self;
}
- (void) test:(NSNotification*) notification
{
NSLog(@"%@", notification);
label.text = notification.object;
[label sizeToFit];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end