Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2010, 04:52 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 20
Sniark is on a distinguished road
Default Loading RSS feed items in a table view... Please HELP :)

I am still stuck with loading some RSS data in a table view. None of the template I found do what I want and I'm managing in amending them...
Is there an easy way to populate data in a table view from a RSS feed?
I mean I have a home screen based on a view-based application with a clean artwork. Then, I have 3 buttons leading to 3 different setions.

Then, on each section, I'm trying to load some items into a table from a RSS feed but I'm not managing to do it...

Could someone please walk me through?

I'm really new to iPhone & Objective C dev...

Many thanks to everyone!!!!



Here is one .h on one of the 3 sections

Code:
#import <UIKit/UIKit.h>
#import	"MProjectAppDelegate.h"
#import "ArticleViewController.h"

@interface EntreesViewController : UIViewController {

	ArticleViewController *showArticleViewController;
	
	IBOutlet UITableView * newsTable;
	
	UIActivityIndicatorView * activityIndicator;
	
	CGSize cellSize;
	
	NSXMLParser * rssParser;
	
	NSMutableArray * stories;
	
	// a temporary item; added to the "stories" array one at a time, and cleared for the next one
	NSMutableDictionary * item;
	
	// it parses through the document, from top to bottom...
	// we collect and cache each sub-element value, and then save each item to our array.
	// we use these to track each current item, until it's ready to be added to the "stories" array
	NSString * currentElement;
	NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink;
	
	
	IBOutlet UIButton *menu;

	
	IBOutlet UIButton *retourEntrees;
}
-(IBAction)retourEntreesClick:(id)sender;

//	
@property(nonatomic, retain) ArticleViewController *showArticleViewController;



@end

And the .m which goes with it
Code:
#import "EntreesViewController.h"
#import "ArticleViewController.h"


@implementation EntreesViewController
@synthesize showArticleViewController;


-(IBAction)retourEntreesClick:(id)sender
{
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:10.0];
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
							   forView:[self view]
								 cache:YES];
		[self.view removeFromSuperview];
		[UIView commitAnimations];	
	NSLog(@"Exiting view function OK");

}


- (void)viewDidLoad {
	NSLog(@"ViewDidLoad OK");

	// Add the following line if you want the list to be editable
	// self.navigationItem.leftBarButtonItem = self.editButtonItem;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
	NSLog(@"Number of Section OK");

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [stories count];
	NSLog(@"Stories Count OK");

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	NSLog(@"Identifier OK");

	
	//
	//	Setting up the Cell
	//
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// Set up the cell
	int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
	[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
	
	
	//	Setting up the arrow at the end of the line
	[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
	
	return cell;
	//
	// End of Cell definition on the main menu page
	//
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// Navigation logic
	
	int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
	
	
	
	NSString * storySummary = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
	
	//	 storySummary = [storySummary stringByReplacingOccurrencesOfString:@" " withString:@""];
	//	 storySummary = [storySummary stringByReplacingOccurrencesOfString:@"\n" withString:@""];
	//	 storySummary = [storySummary stringByReplacingOccurrencesOfString:@"	" withString:@""];
	
	
	NSLog(@"summary: %@", storySummary);
	// 
	//	Open in a new NIB
	
	if (self.showArticleViewController == nil) {
		ArticleViewController *tmpViewController = [[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:nil];
		self.showArticleViewController = tmpViewController;
		[tmpViewController release];
	} 	 
	
	[self.navigationController pushViewController:showArticleViewController animated:YES];	 
	[self.showArticleViewController.detailDescription  setText:storySummary];
}


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

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	
	if ([stories count] == 0) {
		NSString * path = @"http://mini-marmiton.com/mini-marmiton-rss-provider.php";
		[self parseXMLFileAtURL:path];
	}
	
	cellSize = CGSizeMake([newsTable bounds].size.width, 60);
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}




- (void)parserDidStartDocument:(NSXMLParser *)parser{	
	NSLog(@"found file and started parsing");
	
}

- (void)parseXMLFileAtURL:(NSString *)URL
{	
	stories = [[NSMutableArray alloc] init];
	
    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];
	
    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
	
    [rssParser parse];
	
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]];
	NSLog(@"error parsing XML: %@", errorString);
	
	UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    //NSLog(@"found this element: %@", elementName);
	currentElement = [elementName copy];
	if ([elementName isEqualToString:@"item"]) {
		// clear out our story item caches...
		item = [[NSMutableDictionary alloc] init];
		currentTitle = [[NSMutableString alloc] init];
		currentDate = [[NSMutableString alloc] init];
		currentSummary = [[NSMutableString alloc] init];
		currentLink = [[NSMutableString alloc] init];
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	//NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"item"]) {
		// save values to an item, then store that item into the array...
		[item setObject:currentTitle forKey:@"title"];
		[item setObject:currentLink forKey:@"link"];
		[item setObject:currentSummary forKey:@"summary"];
		[item setObject:currentDate forKey:@"date"];
		
		[stories addObject:[item copy]];
		NSLog(@"adding story: %@", currentTitle);
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	//NSLog(@"found characters: %@", string);
	// save the characters for the current item...
	if ([currentElement isEqualToString:@"title"]) {
		[currentTitle appendString:string];
	} else if ([currentElement isEqualToString:@"link"]) {
		[currentLink appendString:string];
	} else if ([currentElement isEqualToString:@"description"]) {
		[currentSummary appendString:string];
	} else if ([currentElement isEqualToString:@"pubDate"]) {
		[currentDate appendString:string];
	}
	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	
	[activityIndicator stopAnimating];
	[activityIndicator removeFromSuperview];
	
	NSLog(@"all done!");
	NSLog(@"stories array has %d items", [stories count]);
	[newsTable reloadData];
}




- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	NSLog(@"dealloc OK");

	[currentElement release];
	[rssParser release];
	[stories release];
	[item release];
	[currentTitle release];
	[currentDate release];
	[currentSummary release];
	[currentLink release];
	
	[super dealloc];
}


@end

Thank you very much to everyone
Sniark is offline   Reply With Quote
Old 03-23-2010, 02:33 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 84
vikinara is on a distinguished road
Default check this

iPhone SDK Tutorial: Build a Simple RSS reader for the iPhone


hope it helps
Viki



Quote:
Originally Posted by Sniark View Post
I am still stuck with loading some RSS data in a table view. None of the template I found do what I want and I'm managing in amending them...
Is there an easy way to populate data in a table view from a RSS feed?
I mean I have a home screen based on a view-based application with a clean artwork. Then, I have 3 buttons leading to 3 different setions.

Then, on each section, I'm trying to load some items into a table from a RSS feed but I'm not managing to do it...

Could someone please walk me through?

I'm really new to iPhone & Objective C dev...

Many thanks to everyone!!!!



Here is one .h on one of the 3 sections

Code:
#import <UIKit/UIKit.h>
#import	"MProjectAppDelegate.h"
#import "ArticleViewController.h"

@interface EntreesViewController : UIViewController {

	ArticleViewController *showArticleViewController;
	
	IBOutlet UITableView * newsTable;
	
	UIActivityIndicatorView * activityIndicator;
	
	CGSize cellSize;
	
	NSXMLParser * rssParser;
	
	NSMutableArray * stories;
	
	// a temporary item; added to the "stories" array one at a time, and cleared for the next one
	NSMutableDictionary * item;
	
	// it parses through the document, from top to bottom...
	// we collect and cache each sub-element value, and then save each item to our array.
	// we use these to track each current item, until it's ready to be added to the "stories" array
	NSString * currentElement;
	NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink;
	
	
	IBOutlet UIButton *menu;

	
	IBOutlet UIButton *retourEntrees;
}
-(IBAction)retourEntreesClick:(id)sender;

//	
@property(nonatomic, retain) ArticleViewController *showArticleViewController;



@end

And the .m which goes with it
Code:
#import "EntreesViewController.h"
#import "ArticleViewController.h"


@implementation EntreesViewController
@synthesize showArticleViewController;


-(IBAction)retourEntreesClick:(id)sender
{
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:10.0];
		[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
							   forView:[self view]
								 cache:YES];
		[self.view removeFromSuperview];
		[UIView commitAnimations];	
	NSLog(@"Exiting view function OK");

}


- (void)viewDidLoad {
	NSLog(@"ViewDidLoad OK");

	// Add the following line if you want the list to be editable
	// self.navigationItem.leftBarButtonItem = self.editButtonItem;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
	NSLog(@"Number of Section OK");

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [stories count];
	NSLog(@"Stories Count OK");

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	NSLog(@"Identifier OK");

	
	//
	//	Setting up the Cell
	//
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// Set up the cell
	int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
	[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
	
	
	//	Setting up the arrow at the end of the line
	[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
	
	return cell;
	//
	// End of Cell definition on the main menu page
	//
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// Navigation logic
	
	int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
	
	
	
	NSString * storySummary = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
	
	//	 storySummary = [storySummary stringByReplacingOccurrencesOfString:@" " withString:@""];
	//	 storySummary = [storySummary stringByReplacingOccurrencesOfString:@"\n" withString:@""];
	//	 storySummary = [storySummary stringByReplacingOccurrencesOfString:@"	" withString:@""];
	
	
	NSLog(@"summary: %@", storySummary);
	// 
	//	Open in a new NIB
	
	if (self.showArticleViewController == nil) {
		ArticleViewController *tmpViewController = [[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:nil];
		self.showArticleViewController = tmpViewController;
		[tmpViewController release];
	} 	 
	
	[self.navigationController pushViewController:showArticleViewController animated:YES];	 
	[self.showArticleViewController.detailDescription  setText:storySummary];
}


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

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	
	if ([stories count] == 0) {
		NSString * path = @"http://mini-marmiton.com/mini-marmiton-rss-provider.php";
		[self parseXMLFileAtURL:path];
	}
	
	cellSize = CGSizeMake([newsTable bounds].size.width, 60);
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}




- (void)parserDidStartDocument:(NSXMLParser *)parser{	
	NSLog(@"found file and started parsing");
	
}

- (void)parseXMLFileAtURL:(NSString *)URL
{	
	stories = [[NSMutableArray alloc] init];
	
    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];
	
    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
	
    [rssParser parse];
	
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]];
	NSLog(@"error parsing XML: %@", errorString);
	
	UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    //NSLog(@"found this element: %@", elementName);
	currentElement = [elementName copy];
	if ([elementName isEqualToString:@"item"]) {
		// clear out our story item caches...
		item = [[NSMutableDictionary alloc] init];
		currentTitle = [[NSMutableString alloc] init];
		currentDate = [[NSMutableString alloc] init];
		currentSummary = [[NSMutableString alloc] init];
		currentLink = [[NSMutableString alloc] init];
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	//NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"item"]) {
		// save values to an item, then store that item into the array...
		[item setObject:currentTitle forKey:@"title"];
		[item setObject:currentLink forKey:@"link"];
		[item setObject:currentSummary forKey:@"summary"];
		[item setObject:currentDate forKey:@"date"];
		
		[stories addObject:[item copy]];
		NSLog(@"adding story: %@", currentTitle);
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	//NSLog(@"found characters: %@", string);
	// save the characters for the current item...
	if ([currentElement isEqualToString:@"title"]) {
		[currentTitle appendString:string];
	} else if ([currentElement isEqualToString:@"link"]) {
		[currentLink appendString:string];
	} else if ([currentElement isEqualToString:@"description"]) {
		[currentSummary appendString:string];
	} else if ([currentElement isEqualToString:@"pubDate"]) {
		[currentDate appendString:string];
	}
	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	
	[activityIndicator stopAnimating];
	[activityIndicator removeFromSuperview];
	
	NSLog(@"all done!");
	NSLog(@"stories array has %d items", [stories count]);
	[newsTable reloadData];
}




- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	NSLog(@"dealloc OK");

	[currentElement release];
	[rssParser release];
	[stories release];
	[item release];
	[currentTitle release];
	[currentDate release];
	[currentSummary release];
	[currentLink release];
	
	[super dealloc];
}


@end

Thank you very much to everyone :D
vikinara is offline   Reply With Quote
Old 05-16-2010, 09:32 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 29
Bisbo is on a distinguished road
Default

I've just released an open source RSS/Atom Parser for iPhone and hopefully it might be of some use.

MWFeedParser on GitHub

I'd love to hear your thoughts on it too!
Bisbo is offline   Reply With Quote
Reply

Bookmarks

Tags
rss feed, subview, view-based

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 350
11 members and 339 guests
akacaj, c2matrix, cgokey, esoteric, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,651
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Mirotion22
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0