The goal of my application is to parse some XML, display one element of the xml in a tableview and then have the user click on the cell, and it pushes to a new table view where the rest of the data for that element is displayed. The first table view that displays only the list works fine, but for some reason I can’t get the push to a new view controller to work. Below is the code I am using. The first view controller displays in a table view; currentCallType. I wan the user to be be able to click on that table cell, then push to a new table view where it displays the units, station and location. Any help would be AWESOME! thanks.
View controller for first tableview:
#import "ThirdViewController.h"
#import "UnitsXMLParser.h"
#import "DetailViewController"
@implementation ThirdViewController
@synthesize unitsTableView;
@synthesize currentCallType, station, location, units;
XMLParser1 *xmlParser;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[xmlParser units] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Units *currentCall = [[xmlParser units] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Set up the cell...
cell.textLabel.text = [currentCall currentCallType];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
xmlParser = [[XMLParser1 alloc] loadXMLByURL:@"http://localhost:8888/units.xml"];
}
XML Parser:
#import <Foundation/Foundation.h>
#import "UnitsXMLParser.h"
@implementation XMLParser1
@synthesize units = _units;
NSMutableString *currentNodeContent;
NSXMLParser *parser;
Units *currentCall;
bool isStatus;
-(id) loadXMLByURL:(NSString *)urlString
{
_units = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByReplacingOccurrencesOfString:@"~" withString:@""];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"station"])
{
currentCall = [Units alloc];
isStatus = YES;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:@"units"])
{
currentCall.units = currentNodeContent;
}
if ([elementname isEqualToString:@"name"])
{
currentCall.currentCallType = currentNodeContent;
}
if ([elementname isEqualToString:@"stationid"])
{
currentCall.station = currentNodeContent;
}
if ([elementname isEqualToString:@"address"])
{
currentCall.location = currentNodeContent;
}
}
if ([elementname isEqualToString:@"station"])
{
[self.units addObject:currentCall];
currentCall = nil;
currentNodeContent = nil;
}
}
@end
A longshot; but do you have your project configured to use a
UINavigationController? Your view controllers will not be contained in / managed by a UINavigationController unless you start with the correct project template or manually add theUINavigationController. If the view controller containing your posted code is not backed by a navigation controller,[self.navigationController pushViewController:detail animated:YES]will do nothing.Also, make sure that you have hooked up your
UITableViewDelegatein addition to yourUITableViewDataSource, so that your code will catch the row selection.I only ask because you do not explicitly state that you have set up your project to use a
UINavigationControlleror confirm that you have hooked up both the delegate and datasource in Interface Builder.