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.
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
Full copy of my header file (table controller)
the WGTempTableController class is UIViewController with table outlet, data array etc ...
//
// CRFeedController.h
// czReader
//
// Created by Ondrej Rafaj on 5.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WGTempTableController.h"
#import <IGDataRss20.h>
@interface CRFeedController : WGTempTableController <IGDataRss20Delegate> {
//NSString *startUrl;
}
@end
Full copy of my implementation file (table controller)
All other functions like numberOfSectionsInTableView or numberOfRowsInSection are in that WGTempTableController
//
// CRFeedController.m
// czReader
//
// Created by Ondrej Rafaj on 5.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import "CRFeedController.h"
#import "WGTempCell.h"
@implementation CRFeedController
- (void)viewDidLoad {
[super viewDidLoad];
IGDataRss20 *rss = [[[IGDataRss20 alloc] init] autorelease];
rss.delegate = self;
[rss initWithContentsOfUrl:@"http://rss.cnn.com/rss/cnn_topstories.rss"];
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (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");
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
WGTempCell *cell = (WGTempCell *) [table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CRFeedCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[WGTempCell class]]) {
cell = (WGTempCell *) currentObject;
break;
}
}
}
NSDictionary *d = [super.data objectAtIndex:indexPath.row];
[[cell cellTitle] setText:[d objectForKey:@"title"]];
return cell;
}
- (void)dealloc {
[super dealloc];
}
@end
Full copy of my header file (rss reader)
//
// IGDataRss20.h
// IGFrameworkProject
//
// Created by Ondrej Rafaj on 4.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import <Foundation/Foundation.h>
@class IGDataRss20;
@protocol IGDataRss20Delegate <NSObject>
@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 <IGDataRss20Delegate> delegate;
- (void)initWithContentsOfUrl:(NSString *)rssUrl;
- (void)initWithContentsOfData:(NSData *)inputData;
@end
Full copy of my implementation file (rss reader)
//
// IGDataRss20.m
// IGFrameworkProject
//
// Created by Ondrej Rafaj on 4.4.10.
// Copyright 2010 Home. All rights reserved.
//
#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