views:

74

answers:

2

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
A: 

It looks to me, as if you initialize the NSMutableArray data twice: First in initWithContentsOfUrl: , then again in parsingEnded: . Maybe you should do a removeAllObjects in parsingEnded instead.

Frank Martin
That's not it unfortunately, I am getting the NSLog(@"Parsing ended"); message, I can see the data in the table, I can even click on the row but when I try to scroll the table it crashes ...
Ondrej
it even crashes when I do disable everything in - (void)parsingEnded:(NSArray *)result { //super.data = [[NSMutableArray alloc] initWithArray:result]; //NSLog(@"My Items: %d", [super.data count]); //[super.table reloadData]; NSLog(@"Parsing ended");}
Ondrej
end it works only if I disable the rss.delegate = self; row :(
Ondrej
HELP PLEASE SOMEONE :)))
Ondrej
+3  A: 

Your subject says it crashes when you try to scroll. I don't know what this would have to do with your rss.delegate, so I'm just going to ignore that and focus on probable scrolling-related bugs here, which are usually in tableView:cellForRowAtIndexPath:.

  1. Check your CRFeedCell.xib, view info on your WGTempCell object, and make sure its Identifier field matches your CellIdentifier in your code. ("MyCell")

  2. Make sure you're not using that same CellIdentifier for some other UITableViewCell subclass elsewhere in your code.

  3. What kind of crash is it? If it's EXC_BAD_ACCESS, double-click on your executable, go to Arguments, create an NSZombieEnabled environment variable, and set it to YES. (Uncheck it when you're done debugging to avoid leaking memory.) This will show you which object you were trying to access when the app crashed.

  4. Set a breakpoint on the setText: call in tableView:cellForRowAtIndexPath:. Then at your gdb prompt, type po [d objectForKey:@"title"]. Make sure that object is really an NSString.

cduhn
OK, thank you very much. As I found out after enabling zombie mode "I love that name btw" that I am trying to access deallocated object (-[CRFeedController respondsToSelector:]: message sent to deallocated instance 0x1055e20) after CMD+F search for "autorelease" I found out that initialization of my rss class is releasing itself when the viewDidLoad ends. that means that everytime I've tried to scroll again I couldn't get this object again. To be able to release this object I have to initialize this object in the header file and release it in dealloc function.Once again, Thanks.Ondrej
Ondrej