tags:

views:

155

answers:

4

I'm new to objective c and iPhone programming. I can't seem to figure out why no cells will fill when I run this code. The xml content displays in the console log and xcode displays no errors. Can any help?

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if(![elementName compare:@"Goal"] )
    {
        tempElement = [[xmlGoal alloc] init];
    }
    else if(![elementName compare:@"Title"])
    {
        currentAttribute = [NSMutableString string];
    }
    else if(![elementName compare:@"Progress"]) 
    {
        currentAttribute = [NSMutableString string];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if(![elementName compare:@"Goal"]) 
    {
        [xmlElementObjects addObject:tempElement];

    }
    else if(![elementName compare:@"Title"]) 
    {
        NSLog(@"The Title of this event is %@", currentAttribute);
        [tempElement setTitled:currentAttribute];
     }
     else if(![elementName compare:@"Progress"]) 
    {
        NSLog(@"The Progress of this event is %@", currentAttribute);
        [tempElement setProgressed:currentAttribute];
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(self.currentAttribute)
    {
        [self.currentAttribute appendString:string];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [xmlElementObjects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   

}
A: 

I can't seem to figure out why no cells will fill when I run this code.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [xmlElementObjects count];
}

Is this method getting hit? If so, is xmlElementObjects nil? If not, is it returning 0 since there are no elements in the array? If so, is it because your XML parsing code didn't fill the array before the array is being used?

Shaggy Frog
A: 

Another thing you need to check: Did you set the dataSource property of your tableview to the correct object? Other than that, check if your if(![elementName compare:@"Goal"]) is really 'hit' (either by NSLog()ing or putting a breakpoint there.

Another note, and unrelated, you should probably release tempElement after adding it to the xmlElements dictionary.

Alfons
A: 

Set breakpoints in -tableView:numberOfRowsInSection: and -tableView:cellForRowAtIndexPath: to ensure that they're being called. As Alfons mentioned, it may be that your table view's datasource property is not being set.

Some unrelated notes:

  1. Why are you using the statement ![someString compare:someOtherString] for comparison? NSString has a built-in -isEqualToString: method, so you can replace that statement with [someString isEqaulToString:someOtherString]. Your results won't change, but your code will benefit from the added readability.

  2. In -tableView:cellForRowAtIndexPath:, you have the following code:

    UITableViewCell *cell = [mtableview
                              dequeueReusableCellWithIdentifier:CellIdentifier];
    

    This might cause problems if you keep using the instance variable (in this case, mtableview) here. The method has a table view as an argument, so use tableView instead. That will ensure that you dequeue a cell from the table view that called this method.

  3. Are you developing for iPhone OS 3.0 or above? If so, the line

    [[UITableViewCell alloc] initWithFrame:CGRectZero
                           reuseIdentifier:CellIdentifier];
    

    should be replaced with

    [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                           reuseIdentifier:CellIdentifier];
    

    using whichever cell style you'd like, naturally.

Jeff Kelley
A: 
Michael Orcutt
Do not reply to your own question with an answer. Edit your original post instead.
Shaggy Frog