1

UITableViewに表示する次のXMLがあります。

<result>
<trip duration="03:30">
<takeoff date="2010-06-19" time="18:40" city="Moscow"/>
<landing date="2010-06-19" time="20:10" city="Novgorod"/>
<flight carrier="Rossiya" number="8395" eq="320"/>
<price>13429.00</price>
</trip>
<trip duration="03:40">
<takeoff date="2010-06-19" time="09:20" city="Omsk"/>
<landing date="2010-06-19" time="11:15" city="Paris"/>
<flight carrier="AirFrance" number="1145" eq="320"/>
<price>13229.00</price>
</trip>
<trip duration="03:50">
<takeoff date="2010-06-19" time="07:20" city="Omsk"/>
<landing date="2010-06-19" time="14:15" city="Barcelona"/>
<flight carrier="AirFrance" number="1100" eq="320"/>
<price>13329.00</price>
</trip>
</result>

私はそれを保存するためにこのコードを使用しています:

if ([elementname isEqualToString:@"trip"]) {
    currentTweet = [Tweet alloc];
    isStatus = YES;
    flightTime = [attributeDict objectForKey:@"duration"];
    currentTweet.flightDuration = [NSString stringWithFormat:@"Flight duration is: %@", flightTime];
}

if([elementname isEqualToString:@"takeoff"]) {

    takeoffPlace = [attributeDict objectForKey:@"city"];
} 
if ([elementname isEqualToString:@"landing"]) {
    landingPlace = [attributeDict objectForKey:@"city"];
    currentTweet.content = [NSString stringWithFormat:@"%@ - %@", takeoffPlace, landingPlace];
}

if ([elementname isEqualToString:@"takeoff"]) {
    takeoffDate = [attributeDict objectForKey:@"date"];
    takeoffTime = [attributeDict objectForKey:@"time"];
    currentTweet.takeOffAll = [NSString stringWithFormat:@"Take off date %@. Take off time %@", takeoffDate, takeoffTime];
}

if ([elementname isEqualToString:@"landing"]) {
    landingDate = [attributeDict objectForKey:@"date"];
    landingTime = [attributeDict objectForKey:@"time"];
    currentTweet.landingAll = [NSString stringWithFormat:@"Landing date %@. Landing time %@", landingDate, landingTime];
}

if ([elementname isEqualToString:@"flight"]) {
    companyName = [attributeDict objectForKey:@"carrier"];
    companyNumber = [attributeDict objectForKey:@"number"];
    eqStuff = [attributeDict objectForKey:@"eq"];
    currentTweet.companyInfo = [NSString stringWithFormat:@"Carrier: %@ Flight number: %@ Eq: %@", companyName, companyNumber, eqStuff];
}

TableViewを2つのパラメーターで並べ替える必要があります。

  1. <trip duration="03:50">時間_

  2. <price>

正しい構造を使用しているかどうかはわかりません。基本的に、すべてのデータは、別のデータクラスに割り当てられたNSStringであり、UITableViewセル内のUILabelに割り当てられます。

時間と価格でデータを並べ替える最良の方法は何でしょうか?コードを再フォーマットする方法について何かアイデアはありますか?

PS私はSegmentedControlを使用してTableViewをリロードし、それに応じて並べ替えています。

4

2 に答える 2

2

それをすべて配列に入れ、ソート後に配列を使用して UITableView をロードする必要があります。

于 2012-11-27T22:37:05.080 に答える
1

sortedArrayUsingComparator結果を辞書エントリの配列に保存し、この回答の最後に示すように配列を並べ替えることができます。

たとえば、私のパーサーは次のとおりです。

@interface XmlParserViewController () <NSXMLParserDelegate>
{
    NSMutableArray *trips;
    NSMutableDictionary *currentTrip;
    NSMutableString *currentElement;
}
@end

@implementation XmlParserViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    trips = [[NSMutableArray alloc] init];

    // clearly I'm reading your XML from a file in my bundle, but you'll get it
    // how ever you're retrieving it from your remote source

    NSString *filename = [[NSBundle mainBundle] pathForResource:@"results" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:filename];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
}

#pragma mark - NSXMLParserDelegate methods

#define kRowElementTag @"trip"

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    NSArray *attributeElementNames = @[@"takeoff", @"landing", @"flight"];
    NSArray *foundCharacterElementNames = @[@"price"];

    if ([elementName isEqualToString:kRowElementTag])
    {
        currentTrip = [[NSMutableDictionary alloc] init];
        [trips addObject:currentTrip];
        if (attributeDict)
            [currentTrip setObject:attributeDict forKey:elementName];
    }
    else if (currentTrip)
    {
        if ([attributeElementNames containsObject:elementName])
        {
            if (attributeDict)
                [currentTrip setObject:attributeDict forKey:elementName];
        }
        else if ([foundCharacterElementNames containsObject:elementName] && currentElement == nil)
        {
            // you can change this to just grab a few fields ... add whatever fields you want to this

            currentElement = [[NSMutableString alloc] init];
            [currentTrip setObject:currentElement forKey:elementName];
        }
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:kRowElementTag])
    {
        currentTrip = nil;
    }
    else if (currentElement)
    {
        currentElement = nil;
    }
}

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

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"%s error=%@", __FUNCTION__, parseError);

    // we should handle the error here
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"Order in XML:");

    for (NSDictionary *trip in trips)
    {
        NSLog(@"    %@ %@ %@ %@ %@ %@", trip[@"flight"][@"carrier"], trip[@"flight"][@"eq"], trip[@"takeoff"][@"date"], trip[@"takeoff"][@"time"], trip[@"trip"][@"duration"], trip[@"price"]);
    }

    NSArray *sortedTrips = [self sortResultsByDurationAndPrice];

    NSLog(@"Sorted order:");

    for (NSDictionary *trip in sortedTrips)
    {
        NSLog(@"    %@ %@ %@ %@ %@ %@", trip[@"flight"][@"carrier"], trip[@"flight"][@"eq"], trip[@"takeoff"][@"date"], trip[@"takeoff"][@"time"], trip[@"trip"][@"duration"], trip[@"price"]);
    }

    // you can now update your user interface or do whatever at this point
    //
    // [self.tableView reloadData];
}

次のようにparserDidEndDocument呼び出していることに気付くでしょう。sortResultsByDurationAndPrice

- (NSArray *)sortResultsByDurationAndPrice
{
    NSArray *sortedTrips = [trips sortedArrayUsingComparator: ^(id trip1, id trip2) {

        double duration1 = [self durationFromString:trip1[@"trip"][@"duration"]];
        double duration2 = [self durationFromString:trip2[@"trip"][@"duration"]];

        double price1 = [trip1[@"price"] doubleValue];
        double price2 = [trip2[@"price"] doubleValue];

        if (duration1 < duration2 || (duration1 == duration2 && price1 < price2)) {
            return (NSComparisonResult)NSOrderedAscending;
        }

        if (duration1 > duration2 || (duration1 == duration2 && price1 > price2)) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        return (NSComparisonResult)NSOrderedSame;
    }];

    return sortedTrips;
}

// this assumes that duration is in the form of "h:mm"

- (double)durationFromString:(NSString *)durationString
{
    NSArray *durationArray = [durationString componentsSeparatedByString:@":"];

    return [durationArray[0] doubleValue] + [durationArray[1] doubleValue] / 60.0;
}

// sort by date and time not used, but was part of my original answer
// so I provide it here for the sake of completeness

- (NSArray *)sortResultsByDateTime
{
    NSArray *sortedTrips = [trips sortedArrayUsingComparator: ^(id trip1, id trip2) {

        NSComparisonResult dateComparison = [trip1[@"takeoff"][@"date"] compare:trip2[@"takeoff"][@"date"]];
        NSComparisonResult timeComparison = [trip1[@"takeoff"][@"time"] compare:trip2[@"takeoff"][@"time"]];

        if (dateComparison == NSOrderedAscending || (dateComparison == NSOrderedSame && timeComparison == NSOrderedAscending)) {
            return (NSComparisonResult)NSOrderedAscending;
        }

        if (dateComparison == NSOrderedDescending || (dateComparison == NSOrderedSame && timeComparison == NSOrderedDescending)) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        return (NSComparisonResult)NSOrderedSame;
    }];

    return sortedTrips;
}

明らかに、sortResults辞書エントリの個人的な配列に基づいて微調整することができますが、これにより、それがどのように機能するかについてのアイデアが得られるはずです.

于 2012-11-27T22:58:32.687 に答える