使用XML数据填充UITableView

时间:2011-07-22 02:14:10

标签: xml ios uitableview nsxmlparser nsdata

我正在从我的服务器解析信息,并且正在下载所有信息,但是他们正在努力弄清楚如何将信息添加到表格视图中。基本上我想知道的是如何获取我所拥有的信息并将其添加到NSMutableArray然后计算该对象并将其添加到表视图但每次我将我的字符串添加到可变数组时我什么也得不到...但是我知道我的日志正在正确解析所有信息,并且我只是出于测试目的放置了一个标签......这是我用来获取信息的代码。如何获取我使用的字符串并填充表格视图?

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


    NSLog(@"Parser Was Called");

    if ([elementName isEqualToString:@"element"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"themeName"]);
    themeNameString = [NSString stringWithFormat:@"%@", [attributeDict 
    objectForKey:@"themeName"]];
    //Test first string
    label.text = themeNameString;
     }
    if ([elementName isEqualToString:@"element1"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"creator"]);
    themeCreator = [NSString stringWithFormat:@"%@", [attributeDict 
    objectForKey:@"creator"]];
    }    
    if ([elementName isEqualToString:@"element2"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"price"]);
    themePrice = [NSString stringWithFormat:@"%@", [attributeDict 
    objectForKey:@"price"]];

    }      
    if ([elementName isEqualToString:@"element3"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"twitterName"]);
    creatorTwitterName = [NSString stringWithFormat:@"%@", [attributeDict 
    objectForKey:@"twitterName"]];

      }  
    if ([elementName isEqualToString:@"element4"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"themeURL"]);
    cydiaLink = [NSString stringWithFormat:@"%@", [attributeDict 
    objectForKey:@"themeURL"]];

     }   
    if ([elementName isEqualToString:@"element5"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"themeDeciption"]);
    deciption = [NSString stringWithFormat:@"%@", [attributeDict 
    objectForKey:@"themeDeciption"]];

    }     

    if ([elementName isEqualToString:@"element6"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"themeScreenShots"]);
    screenShots = [NSString stringWithFormat:@"%@", [attributeDict 
     objectForKey:@"themeScreenShots"]];

    }        
    if ([elementName isEqualToString:@"element7"]) {
    NSLog(@"elements recieved %@", [attributeDict objectForKey:@"promo"]);
    tblViewImage = [NSString stringWithFormat:@"%@", [attributeDict 
     objectForKey:@"promo"]];

    } 

    }

2 个答案:

答案 0 :(得分:0)

你有一些基本的怀疑(没关系,我们都有那些),这就是为什么我认为你应该自己学习完成这项任务(信息=>代表桌面上的信息)。我建议你按照这个教程:

http://www.icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/

它教导如何从NSMutableArray表示表的信息。试试看,如果你有任何疑问,你可以问我,我会帮助你。

答案 1 :(得分:0)

至于现在我只看到你使用

– parser:didStartElement:namespaceURI:qualifiedName:attributes:

要获取数组中的所有xml数据,您应该多一点。这是关于NSXMLParser的documentatinoa回合。

http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html

取决于你想要什么,但我通常使用

– parser:didStartElement:namespaceURI:qualifiedName:attributes:
– parser:didEndElement:namespaceURI:qualifiedName:
– parser:foundCharacters:

但是,再次,它实际上取决于你想要什么,因为有更多的方法可供选择。