UITableView and WebView

Discussion in 'iOS Development' started by maxk96, Jul 26, 2010.

  1. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    Hi, i have a UITableView which parses an rss into the tableview. Please could someone implement in my code how to load a webview on another viewcontroller when one of the rss cells is clicked?

    Thnks
    Max
    Code:
    @implementation RootViewController
    - (void)viewDidLoad {	[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
    	rssTable.backgroundColor = [UIColor blackColor];
    	// Add the following line if you want the list to be editable
    	// self.navigationItem.leftBarButtonItem = self.editButtonItem;
    }
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return 1;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	return [stories count];
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	
    	static NSString *MyIdentifier = @"MyIdentifier";
    	
    	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"]];
    	cell.textLabel.textColor = [UIColor redColor];
    	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	return cell;
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	// Navigation logic
    	
    	int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    	
    	NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
    	
    	// clean up the link - get rid of spaces, returns, and tabs...
    	storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    	storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    	storyLink = [storyLink stringByReplacingOccurrencesOfString:@"	" withString:@""];
    	
    	NSLog(@"link: %@", storyLink);
    	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
    }
    
    
    - (void)viewWillAppear:(BOOL)animated {
    	[super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
    	[super viewDidAppear:animated];
    	
    	if ([stories count] == 0) {
    		NSString * path = @"http://sdk4you.com/?feed=rss2";
    		[self parseXMLFileAtURL:path];
    	}
    	
    	cellSize = CGSizeMake([rssTable 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];
    	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 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"]) {
    		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"]) {
    		[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{
    	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]);
    	[rssTable reloadData];
    }
    
    
    
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations
    	return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    
    - (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 {
    	
    	[currentElement release];
    	[rssParser release];
    	[stories release];
    	[item release];
    	[currentTitle release];
    	[currentDate release];
    	[currentSummary release];
    	[currentLink release];
    	
    	[super dealloc];
    }
    
    
    @end
  2. MountainDew

    MountainDew New Member

    Joined:
    Feb 23, 2008
    Messages:
    194
    Likes Received:
    1
    Device:
    iPod touch
    Lean about navigation controllers.
  3. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    can you just tell me please? i really need help asap
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
  5. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    i know its just a basis.
  6. MountainDew

    MountainDew New Member

    Joined:
    Feb 23, 2008
    Messages:
    194
    Likes Received:
    1
    Device:
    iPod touch
  7. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    i just need some help!!! i know its not goanna help. Iv ordered a load of books from amazon and im downloading a series from iTunes U. This is just one thing i need help with. Is that too much to ask?
  8. MountainDew

    MountainDew New Member

    Joined:
    Feb 23, 2008
    Messages:
    194
    Likes Received:
    1
    Device:
    iPod touch
    It's not a matter of copy and paste, you need to modify the delegate as well. Just read an article or watch a video.
  9. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)

    Given that you have failed to show that you know even the fundamentals of C or Objective-C, and that you are too impatient to wait, learn, and ask intelligent questions (rather than begging for someone to write code for you), yes, that is too much too ask.

    Gojohnnyboi wasn't kidding:

  10. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    i cant find any articles or videos!

Share This Page