开发者

How to display data stored in NSMutableArray in a TableView?

开发者 https://www.devze.com 2023-03-31 05:08 出处:网络
I am new to the iphone programming. I am using XML Parser to receive the data stored in anNSMutableArray and display it in a TableView.

I am new to the iphone programming. I am using XML Parser to receive the data stored in an NSMutableArray and display it in a TableView. When I'm compiling,I can see the data in console using NSLog, but it doesn't show up in the tableView on the simulator. I need your help to work this out please .

    //  XMLParser.m
#import "XMLParser.h"

@implementation XMLParser

- (NSDictionary *)catalogue{
    return catalogue;
}
- (id)parseXMLAtURL:(NSURL *)url
         parseError:(NSError **)error
{
    catalogue = [NSMutableDictionary new];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser parse];
    if([parser parserError] && error) {
        *error = [parser parserError];
    }
    [parser release];
    return self;
}
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
     attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"Catalogue"]) { 
        categories = [NSMutableArray new];
    }else if([elementName isEqualToString:@"Categorie"]) {
        categorie = [NSMutableDictionary new];
        [categorie setValue:[attributeDict valueForKey:@"id"] forKey:@"id"];
        typesProduit = [NSMutableArray new];
    }else if([elementName isEqualToString:@"typeproduit"]) { 
        typeProduit = [NSMutableDictionary new];
        [typeProduit setValue:[attributeDict valueForKey:@"id"] forKey:@"id"];
        produits = [NSMutableArray new];
    }else if([elementName isEqualToString:@"produit"]) { 
        produit = [NSMutableDictionary new];
        [produit setValue:[attributeDict valueForKey:@"id"] forKey:@"id"];
    }else {
        currentNodeName = [elementName copy];
        currentNodeContent = [[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    //NSLog(@"Close tag: %@", elementName); 

    if([elementName isEqualToString:@"Catalogue"]) {
        [catalogue setValue:categories forKey:@"categories"];
        [categories release];
        categories = nil;
    }else if([elementName isEqualToString:@"Categorie"]) {
        [categorie setValue:typesProduit forKey:@"typesProduit"];
        [typesProduit release];typesProduit=nil;
        [categories addObject:categorie];
        [categorie release];categorie = n开发者_运维问答il;
        [typesProduit release];typesProduit=nil;
    }else if([elementName isEqualToString:@"typeproduit"]) {
        [typeProduit setValue:produits forKey:@"produits"];
        [typesProduit addObject:typeProduit];
        [typeProduit release];typeProduit=nil;
        [produits release];produits=nil;
    }else if([elementName isEqualToString:@"produit"]) {
        [produits addObject:produit];
        [produit release];produit=nil;
    }else if([elementName isEqualToString:@"nom"]) {
        [categorie setValue:currentNodeContent forKey:@"nom"];
    }else if([elementName isEqualToString:@"libelle"]) {
        [typeProduit setValue:currentNodeContent forKey:@"libelle"];
    }else {
        [produit setValue:currentNodeContent forKey:elementName];
        [currentNodeContent release];currentNodeContent=nil;
    }
}

- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{   
    [currentNodeContent appendString:string];
}
- (void)dealloc{
    [catalogue release];
    [super dealloc];
}
@end


See the UITableViewController Class Reference. There are also lots of tutorials on creating table views.

You need a class that conforms to the UITableViewDataSource protocol. It can be any class you write yourself, but UITableViewController conforms to the protocol, so you can subclass it then use your subclass as the delegate.

In your delegate you need to define the following method:

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

The table view controller instantiates the table view itself then hands it to you, so you don't need to allocate it itself.

The NSIndexPath is just a pair of integers, one is the index of your table section and the other the index of your cell within that section. A simple table will only have one section, so the first index will always be zero, but you can have as many sections as you want.

You create the UITableViewCell then set the text of your cell's textLabel property to the string you get from one of your array elements. There is also a detailTextLabel that can show additional information in the same cell.

With what I've told you, the Table View Programming Guide for iOS should get you where you want to be.

Note that there are all kinds of problems with table views. Certainly do learn how to implement one and get it working, but if you find that it doesn't do what you want, and you don't have too many cells, consider using a UIScrollView instead.

The advantage of the UITableViewController is that it works for essentially infinite sized tables. It removes cells as they scroll off the top or bottom, puts them in a cache then makes them available for reuse by cellForRowAtIndexPath. That means your table can appear to have millions of rows while remaining responsive and consuming only a little memory. But very few applications really need something that robust.


Try like this,

    - (void)parser:(NSXMLParser *)parser
    foundCharacters:(NSString *)string
    {   
    if(currentNodeContent  == null){
    currentNodeContent = [[NSMutableString alloc] init];
    }
else
        [currentNodeContent appendString:string];
    }

remove currentNodeContent = [[NSMutableString alloc] init]; from this method

- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
     attributes:(NSDictionary *)attributeDict 

I doubt whether currentNodeContent is getting initailised when it is in above method


If your data is read correctly (checked by NSLog) the problem must be in your UITableView datasource.

Check your cellForRowAtIndexPath: method. There you should set the cell label to the appropriate name:

cell.textLabel.text = [yourDataArray objectAtIndex:indexPath.row];

In your parsing routines, fill yourDataArray with the items of your various classes (produit, categorie, etc.) that you want displayed in your table view.

0

精彩评论

暂无评论...
验证码 换一张
取 消