UITableView crashes when trying to scroll
- by Ondrej
Hi,
I have a problem with data in UITableView. I have UIViewController, that contains UITableView outlet and few more things I am using and ... It works :) ... it works lovely, but ...
I've created an RSS reader class that is using delegates to deploy the data to the table ... and once again, If I'll just create dummy data in the main controller everything works!
problem is with this line: rss.delegate = self;
Preview looks a little bit broken than here are those RSS reader files on Google code:
(Link to the header file on GoogleCode)
(Link to the implementation file on Google code)
viewDidLoad function of my controller:
IGDataRss20 *rss = [[[IGDataRss20 alloc] init] autorelease];
rss.delegate = self;
[rss initWithContentsOfUrl:@"http://rss.cnn.com/rss/cnn_topstories.rss"];
and my delegate methods:
- (void)parsingEnded:(NSArray *)result {
super.data = [[NSMutableArray alloc] initWithArray:result];
NSLog(@"My Items: %d", [super.data count]);
[super.table reloadData];
NSLog(@"Parsing ended");
}
(void)parsingError:(NSString *)message {
NSLog(@"MyMessage: %@", message);
}
(void)parsingStarted:(NSXMLParser *)parser {
NSLog(@"Parsing started");
}
Just to clarify, NSLog(@"Parsing ended"); is being executed and I have 10 items in the array.
Ok, here's my RSS reader header file:
@class IGDataRss20;
@protocol IGDataRss20Delegate
@optional
(void)parsingStarted:(NSXMLParser *)parser;
(void)parsingError:(NSString *)message;
(void)parsingEnded:(NSArray *)result;
@end
@interface IGDataRss20 : NSObject {
NSXMLParser *rssParser;
NSMutableArray *data;
NSMutableDictionary *currentItem;
NSString *currentElement;
id <IGDataRss20Delegate> delegate;
}
@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, assign) id delegate;
(void)initWithContentsOfUrl:(NSString *)rssUrl;
(void)initWithContentsOfData:(NSData *)inputData;
@end
And this RSS reader implementation file:
#import "IGDataRss20.h"
@implementation IGDataRss20
@synthesize data, delegate;
(void)initWithContentsOfUrl:(NSString *)rssUrl {
self.data = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:rssUrl];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
(void)initWithContentsOfData:(NSData *)inputData {
self.data = [[NSMutableArray alloc] init];
rssParser = [[NSXMLParser alloc] initWithData:inputData];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
(void)parserDidStartDocument:(NSXMLParser *)parser {
[[self delegate] parsingStarted:parser];
}
(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to parse RSS feed (Error code %i )", [parseError code]];
NSLog(@"Error parsing XML: %@", errorString);
if ([parseError code] == 31) NSLog(@"Error code 31 is usually caused by encoding problem.");
[[self delegate] parsingError:errorString];
}
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) currentItem = [[NSMutableDictionary alloc] init];
}
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[data addObject:(NSDictionary *)[currentItem copy]];
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (![currentItem objectForKey:currentElement]) [currentItem setObject:[[[NSMutableString alloc] init] autorelease] forKey:currentElement];
[[currentItem objectForKey:currentElement] appendString:string];
}
(void)parserDidEndDocument:(NSXMLParser *)parser {
//NSLog(@"RSS array has %d items: %@", [data count], data);
[[self delegate] parsingEnded:(NSArray *)self.data];
}
(void)dealloc {
[data, delegate release];
[super dealloc];
}
@end
Hope someone will be able to help me as I am becoming to be quite desperate, and I thought I am not already such a greenhorn :)
Thanks,
Ondrej