Re: Creating table rows from XML
Re: Creating table rows from XML
- Subject: Re: Creating table rows from XML
- From: Nathan Day <email@hidden>
- Date: Tue, 21 Apr 2009 14:37:03 +1000
What happens when you run it in the debugger, you say this is your
first iPhone application if you are new to using a debugger then you
really need to learn how to use that, it will make finding bugs like
this a lot easier, for example you can break just before you add an
object to you array (pages) and type 'po pages' in the terminal to see
what pages is, if I have miss read you and you are familiar with the
debugger then just ignore everything I have said.
On 20/04/2009, at 4:30 PM, Phil Dokas wrote:
Hello all, I'm writing my first iPhone application and I've run into
a problem I can't seem to get a grasp on.
In short I want to parse an XML file and display a UITableView with
data from a set of XML elements. I've got all the code written for
this but whenever I try to access my array of data inside of -
tableView:cellForRowAtIndexPath: I crash without an error message in
console.
Here are the two relevant classes:
@interface PDPagesViewController : UITableViewController {
NSData* received_data;
NSMutableArray* pages;
}
- (void)beginParsing;
@end
@interface PDPage : NSObject {
NSNumber* page_id;
NSString* title;
}
@property(nonatomic,retain) NSNumber* page_id;
@property(nonatomic,copy) NSString* title;
-(id)init;
-(id)initWithID:(int)in_id andTitle:(NSString*)in_title;
@end
I init my PDPagesViewController as follows:
- (id)init {
if (self = [super init]) {
pages = [[NSMutableArray alloc] init];
[self beginParsing];
}
return self;
}
I construct my NSURLRequest and setup my parser inside of
beginParsing. Here is the delegate method which then builds my array:
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
// Handle <page> elements
if ([elementName isEqualToString:@"page"]) {
NSString *titleAttr = [attributeDict objectForKey:@"title"];
NSString *idAttr = [attributeDict objectForKey:@"id"];
if (titleAttr && idAttr) {
PDPage* newPage = [[PDPage alloc] initWithID:[idAttr intValue]
andTitle:titleAttr];
[pages addObject:newPage];
[newPage release];
}
return;
}
}
Insofar as I can tell, everything is ok so far. The problem occurs
in this table delegate method (again, part of
PDPagesViewController), with crashing lines and received output
listed in comments:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSLog(@"Setting up table cell %d", [indexPath row]); // "Setting
up table cell: 8"
NSLog(@"retain count: %d", [pages retainCount]); // "retain count:
1"
NSLog(@"array count: %d", [pages count]); // "array count: 16"
NSLog(@"page retain count: %d", [(PDPage*)[pages objectAtIndex:
[indexPath row]] retainCount]); // "page retain count: 1"
NSLog(@"title: %@", [(PDPage*)[pages objectAtIndex:[indexPath row]]
title]); // This line causes a crash
NSLog(@"pages array right now: %@", pages); // This line causes a
crash
cell.text = [[pages objectAtIndex:[indexPath row]] title]; // This
line causes a crash
return cell;
}
It seems to me that whenever I try to interact with my
NSMutableArray as such in this method it crashes, but when I use
only its NSObject methods it's fine. And yet, it and its contents
have the right retain count. I feel like I must be missing something
basic, so any insight would be much appreciated. Thank you!
--
Phil Dokas -//- email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden