简体   繁体   English

uitableview部分和xml中的行

[英]uitableview sections and rows from xml

I am really struggling on this. 我真的很努力。 I have an XML feed: 我有一个XML feed:

<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
    <date>
        <name>27/9/10</name>
        <event>
            <title>Event 1</title>
            <more>Copy in here</more>
        </event>
    </date>
    <date>
        <name>04/10/10</name>
        <event>
            <title>Event 1</title>
            <more>Copy in here</more>
        </event>
        <event>
            <title>Second Event</title>
            <more>Copy in here</more>
        </event>
    </date>
</data>

I can manage to parse the xml with: 我可以使用以下方法解析xml:

- (void)parseXMLFileAtURL:(NSString *)URL {
    sections = [[NSMutableArray alloc] init];
    items = [[NSMutableArray alloc] init];

    NSURL *xmlURL = [NSURL URLWithString:URL];
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [rssParser setDelegate:self];
    [rssParser parse];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {

}

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

    currentElement = [elementName copy];

    if ([elementName isEqualToString:@"event"]) {
        currentTitle = [[NSMutableString alloc] init];
        currentSummary = [[NSMutableString alloc] init];
    }

    if ([elementName isEqualToString:@"date"]) {
        item = [[NSMutableDictionary alloc] init];
    currentSection = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if ([elementName isEqualToString:@"date"]) {
        [item setObject:[NSNumber numberWithInt:itemsCount] forKey:@"itemsCount"];
        [item setObject:currentSection forKey:@"name"];
        [items addObject:[item copy]];

        itemsCount = 0;
    }

    if ([elementName isEqualToString:@"event"]) {
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentSummary forKey:@"more"];
        [items addObject:[item copy]];

        itemsCount = itemsCount + 1;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"name"]) {
        [currentSection appendString:string];
    } else if ([currentElement isEqualToString:@"title"]) {
        [currentTitle appendString:string];
    } else if ([currentElement isEqualToString:@"more"]) {
        [currentSummary appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.progressView.view removeFromSuperview];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [dataTable reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([sections count] == 0) {
        NSString * path = @"http://www.campuscloud.co.uk/_dev/calendar.xml";
        [self parseXMLFileAtURL:path];
    }

}

But when I try and display this in my table it all goes wrong when I display more than two events for the one date. 但是,当我尝试在我的表中显示此事件时,当我在一个日期中显示两个以上的事件时,一切都会出错。 I want the date to show as a section header and the events to be displayed below. 我希望日期显示为部分标题,而事件则显示在下面。

Any help on this welcome... 对此欢迎有任何帮助...

Hey buddy.. I don't know if I understood coorect, but let's go.. 嘿,哥们..我不知道我是否了解coorect,但我们走吧。

You can create a dictionary of arrays.. 您可以创建数组的字典。

NSMutableDictionary *dates = [NSMutableDictionary alloc];
NSMutableArray *events = [NSMutableArray alloc];

in the array you will insert all event of one date.. 在数组中,您将插入一个日期的所有事件。

[events addObject:@"event 1"];

in the dictionary you will insert the array of events with tha date as key 在字典中,您将插入以tha date为键的事件数组

[dates setobject:events forKey:@"01/01/2010"];

whenever the parser find a new date, a new array should be created (I mean, the values are new.. don't append the array, clean it.. but you don't need to create a brand new array) 每当解析器找到新日期时,都应创建一个新数组(我的意思是,值是新的。不要追加数组,请清理它。但是不需要创建一个全新的数组)

of course that you have to make some changes on the parser to identify the dates and etc, but it's not a big deal. 当然,您必须在解析器上进行一些更改以标识日期等,但这并不重要。

in your table each section is one key from your dictionary.. so the number of sections is the number of items on the dictionary.. and the values inside the section is the array of each date.. 在表中,每个部分是字典中的一个键..因此,部分数是字典中的项数..而该部分中的值是每个日期的数组。

I hope it helps.. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM