У меня есть большая проблема на XCode 4 / Objective C и мой NSXMLParser:
Я получил приложение Tabbar с NSXMLParser на одной вкладке, поэтому при первом вызове ..., когда вкладка становится нажатой/ touch, NSXMLParser анализирует данные в табличном представлении.Но, если на вкладку нажимают / касаются во второй раз, Parser / приложение создает большую утечку памяти, и, если этого недостаточно, происходит сбой на iphone (не на симуляторе).
Может быть, у кого-нибудь из вас есть идеи.Я слышал об обходном пути, но я не знаю, как это реализовать.Любой пример кода и т. Д. Приветствуется.
Я надеюсь, что вы можете мне помочь.спасибо
XMLController.h
#import "XMLController.h"
#import "Tabelle.h"
@implementation XMLController
@synthesize table;
-(id) loadXMLbyURL:(NSString *)urlString{
table = [[NSMutableArray alloc] init];
NSURL *url= [NSURL URLWithString:urlString];
NSData *xmlData = [NSData dataWithContentsOfURL:url];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
parser.delegate = self;
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
return self;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)
namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"team"])
{
[currentNodeContent setString:nil];
currentTabelle = [[Tabelle alloc] init];
currentNodeContent = [[NSMutableString alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)
namespaceURI qualifiedName:(NSString *)qName
{
NSString *theContent = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //
[currentNodeContent setString:@""];
if([elementName isEqualToString:@"pos"])
{
currentTabelle.place = theContent;
}
if([elementName isEqualToString:@"name"])
{
currentTabelle.Name = theContent;
}
if([elementName isEqualToString:@"w"])
{
currentTabelle.wins = theContent;
}
if([elementName isEqualToString:@"l"])
{
currentTabelle.losses = theContent;
}
if([elementName isEqualToString:@"pct"])
{
currentTabelle.winpct = theContent;
}
if([elementName isEqualToString:@"gb"])
{
currentTabelle.GamesBehind = theContent;
}
if([elementName isEqualToString:@"team"])
{
[table addObject:currentTabelle];
[currentTabelle release];
currentTabelle = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
-(void) parserDidEndDocument:(NSXMLParser *)nparser
{
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//currentNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[currentNodeContent appendString:string];
}
@end
#
И класс, где я это называю:
#import "TabellenController.h"
#import "Tabelle.h"
#import "Baseball_DeutschlandAppDelegate.h"
#import "CustomCell.h"
@implementation TabellenController
@synthesize xmlcont, ergebnisTabelle;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
//Abfragen nach der Liga (mit Dateiladen)
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/CellContent.txt",
documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
self.xmlcont = [[XMLController alloc] init];
if ([content isEqualToString:@"1. Bundesliga"]){
if(pageNumber == 0){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=12&xml"];
}
if(pageNumber == 1){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=11&xml"];
}
}if ([content isEqualToString:@"2. Bundesliga"]){
if(pageNumber == 0){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=22&xml"];
}
if(pageNumber == 1){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=21&xml"];
}
}if ([content isEqualToString:@"Regionalligen"]){
if(pageNumber == 0){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=31&xml"];
}
if(pageNumber == 1){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=32&xml"];
}
if(pageNumber == 2){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=33&xml"];
}
if(pageNumber == 3){
[self.xmlcont loadXMLbyURL:@"http://results.baseball-softball.de/extern/standing.php?l=34&xml"];
}
}
[content release];
[super viewDidLoad];
}
- (id)initWithPageNumber:(int)page{
pageNumber = page;
return self;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
/*NSMutableArray *ar = [xmlcont table];
NSInteger *sections;
for (int i = 0; i < [ar count]; i++){
for (int j = (i+1); j < [ar count]; j++){
}
}*/
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSMutableArray *ar = [xmlcont table];
return [ar count];
}
- (NSString *)tableview:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"# Team W L AVG GB"];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
// Configure the cell...
NSMutableArray *cont = [xmlcont table];
Tabelle *current = [cont objectAtIndex:indexPath.row];
cell.place.text = [NSString stringWithFormat:@"%@.", [current place]];
cell.team.text = [NSString stringWithFormat:@"%@", [current Name]];
cell.wins.text = [NSString stringWithFormat:@"%@", [current wins]];
cell.losses.text = [NSString stringWithFormat:@"%@", [current losses]];
cell.winpct.text = [NSString stringWithFormat:@"%@", [current winpct]];
cell.gb.text = [NSString stringWithFormat:@"%@", [current GamesBehind]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 35.0f;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];*/
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
NSLog(@"View verschwindet1");
}
- (void)dealloc {
[self.xmlcont release];
[super dealloc];
}
@end