UITable view,images,rssfeeds - objective-c

how to add an image on the table view header....where the image is read from rss feed and stored in the array called item below is the code
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 69.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 69.0)];
headerView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: #"1" ofType: #"jpg"]]];
return headerView;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict copy];
item = [[NSMutableDictionary alloc] init];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([elementName isEqualToString:#"enclosure"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
if ([elementName isEqualToString:#"itunes:image"])
{
currentImage = [attributeDict objectForKey:#"href"];
[item setObject:currentImage forKey:#"href"];
// NSLog(#"the item current string:%#",item);
NSString *imagefile1 = [item objectForKey:#"href"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
NSLog(#"here we go dis is awesome:%#",image1);
//cell.imageView.image=image1;
image.image=image1;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}

parse XML
Download Image to disk
load Image into ImageView
add imageView to headerView
Each of this step is well documented on SO, developer.apple.com and elsewhere.

Related

Objective-C Pull to Refresh

I'm trying to implement a pull-to-refresh feature on my RSS feed table. The list pulls normally when loading the app, but I essentially need to replicate that for the pull-down.
Could anyone help as to why the code isn't working?
#import "RSSTableViewController.h"
#import "RSSDetailViewController.h"
#interface RSSTableViewController () {
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSString *element;
}
#end
#implementation RSSTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// RSS Settings
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"http://external.example.co.uk/newpost/example.rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self
action:#selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
- (void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing news feed..."];
// Refresh Logic
NSURL *url = [NSURL URLWithString:#"http://external.example.co.uk/newpost/example.rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
// Set the timestamp
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:#"Last updated: %#",
[formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refresh endRefreshing];
[self.tableView reloadData];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[refreshView endRefreshing]; //'Use of undeclared identifier 'refresh view'//
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey:#"title"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:#"item"]) {
item = [[NSMutableDictionary alloc]init];
title = [[NSMutableString alloc]init];
link = [[NSMutableString alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
[item setObject:title forKey:#"title"];
[item setObject:link forKey:#"link"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:#"title"]) {
[title appendString:string];
}
else if ([element isEqualToString:#"link"]) {
[link appendString:string];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey:#"link"];
[[segue destinationViewController] setUrl:string];
}
}
#end
Anyone got any ideas?
Code works fine
Needed to remove the line in parserDidEndDocument with the error.
I then had to add [feeds removeAllObjects] under the //Refresh Logic line to clear the current table contents.
Thanks guys.

NULL Value instead of UILabel and UIButton title in reading xml file (Objective-C)

I have a UILabel and UIButton in different pages, I want to read their title from xml file,
but I got NULL Value, In my XML parser class when I used NSLOG I have my answer but when I want to set them I have NULL, Would you please help me in this implementation?
Thanks in advance!
my XML Parser :
- (Presentation1NameXML *) initXMLParser {
appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"Presentation1Name"]) {
appDelegate.books = [[NSMutableArray alloc] init];
NSLog(#"The Prices Count");
}
else if ([elementName isEqualToString:#"Presentation"]) {
presentation = [[Presentation alloc] init];
presentation.pLabel = [attributeDict objectForKey:#"label"];
NSLog(#"khengggg %#",presentation.pLabel);
// }else if ([elementName isEqualToString:#"Slides"]){
// slides = [[Slide alloc] init];
}
if([elementName isEqualToString:#"slide"]) {
slid = [[Slide alloc] init];
NSLog(#"Processing Element: %#", elementName);
slid.index = [[attributeDict objectForKey:#"index"] integerValue];
NSLog(#"Reading index value :%i", slid.index);
slid.sLabel = [attributeDict objectForKey:#"label"];
NSLog(#"Reading label value :%#", slid.sLabel);
slid.identifier = [attributeDict objectForKey:#"identifier"];
NSLog(#"Reading identifier value :%#", slid.identifier);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Presentation1Name"]){
return;
}
if([elementName isEqualToString:#"Presentation"]) {
[appDelegate.books addObject:presentation];
presentation = nil;
}
// if ([elementName isEqualToString:#"Slides"]){
// [appDelegate.books addObject:slides];
// slides = nil;
// return;
// }
if ([elementName isEqualToString:#"slide"]){
[appDelegate.books addObject:slid];
slid = nil;
}else
[presentation setValue:currentElementValue forKey:#"pLabel"];
//[presentation setValue:currentElementValue forKey:elementName];
currentElementValue = nil;
}
my label code : I got (null)
appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
label.textColor = [UIColor greenColor];
Presentation *p1 = [appDelegate.books objectAtIndex:0];
NSLog(#"aaaaaaaaa aaaa %#", p1);
label.text = p1.pLabel;
My Button code : I got (null)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
Slide *s1 = [appDelegate.books objectAtIndex:0];
NSLog(#" test %#", s1.sLabel);
My Object class Presentation link
My Object class Slide link
My XML file
The problem is here
if ([elementName isEqualToString:#"Presentation1Name"]) {
appDelegate.books = [[NSMutableArray alloc] init];
NSLog(#"The Prices Count");
}
There is no such tag (Presentation1Name) and the array will never be created.
If you want to check NULL values, you can use this code
[str isEqual:[NSNull null]
This returns you if that value iS NULL from web services.
Hope this helps you!
modify your code in function didStartElement,there is no this tag 'Presentation1Name'
if ([elementName isEqualToString:#"Presentation"]) {
appDelegate.books = [[NSMutableArray alloc] init];
NSLog(#"The Prices Count");
}
and In function didEndElement you should delete this line :
[presentation setValue:currentElementValue forKey:#"pLabel"];
and also some memory leaks,you should release after addObject
if([elementName isEqualToString:#"Presentation"]) {
[appDelegate.books addObject:presentation];
[presentation release];
presentation = nil;
}
if ([elementName isEqualToString:#"slide"]){
[appDelegate.books addObject:slid];
[slid release];
slid = nil;
}
you should release
hope it helpful..

Memory leaks while using NSXMLParser

I am having some difficulty dealing with memory leaks in the following code.
Using the leaks instrument within XCode, which shows up the memory leaks within some of my code that is used for rss parsing.
I am using XCode 4, and releasing the allocations at the foot of the code. I have tried adding releases to each local section which causes crashes or the program to stop working.
Any help of advice much appreciated!!
The code which causes the leaks:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to download story feed from web site (Error code %i )", [parseError code]];
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 {
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) {
item = [[NSMutableDictionary alloc] init];
currentImage = [[NSMutableString 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 {
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentImage forKey:#"media"];
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([currentElement isEqualToString:#"media"]) {
[currentImage appendString:string];
} else 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];
}
}
And the releasing later on:
- (void)dealloc {
[currentElement release];
[rssParser release];
[stories release];
[item release];
[currentImage release];
[currentTitle release];
[currentDate release];
[currentSummary release];
[currentLink release];
[super dealloc];
}
Change the following:
UIAlertView * errorAlert = [[[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
This code should be executed only once:
item = [[NSMutableDictionary alloc] init];
currentImage = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
At the start of each didStartElement: method new instances are boing created yet only releases once when the class is dealloc'ed. Thus, as suing that didStartElement: is called more than once there is a build-up of instances of the string objects.
Probably what you want is to create these instances once at the instantiation of the class and then append to them as elements are encountered.
In any event release for each allocation.

Adding activity indicator on webview

i m parsing an rss feed and loading in a webview...wat i want is ..to place a custom activity indicator in the exact place..where the parsing begins and the place where the parsing ends....below is the code.
#implementation MenuAndWineListViewController
NSDictionary *dict;
UIAlertView * errorAlert;
- (void)viewDidLoad
{
self.title=#"Menu & WineList";
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
activityIndicator1 = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator1.frame=CGRectMake(0.0,0.0, 40.0, 40.0);
activityIndicator1.center=self.view.center;
[self.view addSubview:activityIndicator1];
NSURL *baseURL=[[NSURL
URLWithString:#"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com /?feedpages&max=0&sort_order=ASC&parent=12&child_of=12"]retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
if ([stories count] == 0) {
path = #"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com/?feedpages&max=0&
sort_order=ASC&parent=180&child_of=180";
[self parseXMLFileAtURL:path];
}
[menuAndWineListViewController loadHTMLString:[dict objectForKey:#"description"] baseURL:nil];
[menuAndWineListViewController setClipsToBounds:YES];
menuAndWineListViewController.opaque=NO;
menuAndWineListViewController.backgroundColor=[UIColor clearColor];
[menuAndWineListViewController setDelegate:self];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(#"found file and started parsing");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"])
{
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"item"])
{
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentSummary forKey:#"description"];
[stories addObject:[item copy]];
}
for (i=0 ; i<stories.count;i++)
{
dict = [stories objectAtIndex:i];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"title"])
{
[currentTitle appendString:string];
}
else if ([currentElement isEqualToString:#"description"])
{
[currentSummary appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[activityIndicator1 stopAnimating];
[activityIndicator1 removeFromSuperview];
NSLog(#"stories array has %d items", [stories count]);
}
I'd put the
[activityIndicator1 startAnimating];
at the beginning of the parseXMLFileAtURL method
and the
[activityIndicator1 stopanimating];
at the beginning of the parserDidEndDocument like you did.

UITable view,loading images from rss feed

I m reading the xml images from rss feed and parsing it in UITable view. Everything works fine, but it takes time to load the image content in the table view. The screen remains frozen. I'm using NSXMLParser to parse the image. Could you guys help me out, I'd be really greateful. Below is the code.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict 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];
currentString=[[NSMutableString alloc] init];
//currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"url"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
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];
}
else if ([currentElement isEqualToString:#"content:encoded"]) {
[currentSummary appendString:string];
}
}
NSString *imagefile1 = [[stories objectAtIndex:indexPath.row]objectForKey:#"url"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
cell.imageView.image=image1;
[image1 release];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines=2;
cell.textLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"title"];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"pubDate"];
Use Lazy Loading to load images....
somewhat dated but should put you in the right direction
http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/