我是新來這個論壇和iPhone開發。 由於我知道沒有人做iPhone開發,所以我被卡住了,不得不問你們一個解決方案。iPhone:我卡在我的部分在UITableView,數據來自XMLParser
我正在製作一個應用程序,從XMLParser獲取數據,並在這些部分的行(段)內爲段(月)內的UITableView提供數據。
當我運行我的腳本時,它不會這樣做。我知道我接近解決方案,但我看不到它。而沒有人來運行我的腳本使其難以調試。
我(演示)XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Stages>
<Month id="1" name="January">
<Stage id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
</Stage>
<Stage id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary>How the scientific revolution began</summary>
</Stage>
<Stage id="3">
<title>Angels and Demons</title>
<author>Dan Brown</author>
<summary>Robert Langdon is summoned to a Swiss research facility to analyze a cryptic symbol seared into the chest of a murdered physicist.</summary>
</Stage>
</Month>
<Month id="2" name="February">
<Stage id="4">
<title>Keep the Aspidistra Flying</title>
<author>George Orwell</author>
<summary>A poignant and ultimately hopeful look at class and society, Keep the Aspidistra Flying pays tribute to the stubborn virtues of ordinary people who keep the aspidistra flying.</summary>
</Stage>
</Month>
</Stages>
我XMLParser的類:
#import <UIKit/UIKit.h>
@class DAFAppDelegate, Stage, Month;
@interface XMLParser : NSObject <NSXMLParserDelegate>
{
NSMutableString *currentElementValue;
DAFAppDelegate *appDelegate;
Stage *aStage;
Month *aMonth;
}
- (XMLParser *) initXMLParser;
@end
#import "XMLParser.h"
#import "DAFAppDelegate.h"
#import "Stage.h"
#import "Month.h"
@implementation XMLParser
- (XMLParser *) initXMLParser
{
[super init];
appDelegate = (DAFAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Stages"])
{
//Initialize the array.
appDelegate.stages = [[NSMutableArray alloc] init];
}
if([elementName isEqualToString:@"Month"])
{
//Initialize the Month.
aMonth = [[Month alloc] init];
//Extract the attribute here.
aMonth.name = [attributeDict valueForKey:@"name"];
aMonth.monthID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading Month id value :%i", aMonth.monthID);
NSLog(@"Reading Month name value :%@", aMonth.name);
}
if([elementName isEqualToString:@"Stage"])
{
//Initialize the Stage.
aStage = [[Stage alloc] init];
//Extract the attribute here.
aStage.stageID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", aStage.stageID);
}
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:@"Stages"])
return;
//There is nothing to do if we encounter the Stages element here.
//If we encounter the Stage element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:@"Month"])
{
[appDelegate.stages addObject:aMonth];
[aMonth release];
aMonth = nil;
}
if([elementName isEqualToString:@"Stage"])
{
[aMonth.monthStage addObject:aStage];
[aStage release];
aStage = nil;
}
else
{
[aStage setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
}
- (void) dealloc
{
[aStage release];
[aMonth release];
[currentElementValue release];
[super dealloc];
}
@end
我的RootViewController的:
@class DAFAppDelegate; //Here is my stages mutable array defined
@interface RootViewController : UITableViewController
{
DAFAppDelegate *appDelegate;
}
@end
#import "RootViewController.h"
#import "DAFAppDelegate.h"
#import "DetailViewController.h"
#import "Stage.h"
#import "Month.h"
#import "AgendaCustomCell.h"
@implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (DAFAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title = NSLocalizedString(@"Agenda", @"Master view navigation title");
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,45,45)] ;
[image setImage:[UIImage imageNamed:@"topBarIcon.png"]];
[self.navigationController.navigationBar.topItem setTitleView:image];
self.tableView.backgroundColor = [UIColor clearColor];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [appDelegate.stages count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Month Name";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [appDelegate.stages count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"AgendaCustomCell";
AgendaCustomCell *cell = (AgendaCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"AgendaCustomCell" owner:nil options:nil];
for (id currentObject in topLevelObject)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (AgendaCustomCell *)currentObject;
break;
}
}
}
UIView *cellBackView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"customCellBg.png"]];
cell.backgroundView = cellBackView;
Month *aMonth = [appDelegate.stages objectAtIndex:indexPath.section];
Stage *aStage = [aMonth.monthStage objectAtIndex:indexPath.row];
cell.titleLabel.text = aStage.title;
cell.dateLabel.text = aStage.author;
cell.nameLabel.text = aStage.summary;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
#pragma mark -
#pragma mark Table view selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//When a row is selected, create the detail view controller and set its detail item to the item associated with the selected row.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.stage = [appDelegate.stages objectAtIndex:indexPath.row];
// Push the detail view controller.
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc
{
[appDelegate release];
[super dealloc];
}
@end
請幫我在這一個。這幾天我一直在爲我打破頭緒,這讓我瘋狂。
感謝高級!!!
我應該如何編寫代碼呢? – iJar 2011-03-21 11:16:06
@Joannes增加了代碼示例。這有幫助嗎? – Andy 2011-03-21 11:18:17
它現在給我一個indexPath.'Use未聲明的標識符indexPath' – iJar 2011-03-21 12:20:20