1

TableView にイベントのリストを表示するアプリケーションで忙しくしています。行/セルごとに、イベントのタイトルと日付を示すイベントがあります。現在の問題は、1 日に 2 つ以上のイベントがある場合、アプリはそれらをセルごとに適切に表示するようになりましたが、ユーザーには日付が 2 回以上繰り返し表示されるため、非効率的で冗長に見えます。だから私の考えは、以前のセルの日付が現在のセルの日付と似ているかどうかを確認することでした。これにより、現在のセルの日付が非表示になります(ユーザーがイベントをカレンダーに保存できるため、完全に削除することはできません)。

TouchXML を介して外部 XML を SQLite データベースに読み込んでいます。

これが私のコードのサンプルです:

-(void) grabXMLData:(NSString *)dataAddress {

    _managedObjectContext = [(DAFAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSURL *url = [NSURL URLWithString: dataAddress];
    CXMLDocument *xmlParser = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];

    NSArray *resultNodes = NULL;
    resultNodes = [xmlParser nodesForXPath:@"//month" error:nil];

    for (CXMLElement *resultElement in resultNodes) {
        NSMutableDictionary *monthItem = [[NSMutableDictionary alloc] init];

        [monthItem setObject:[[resultElement attributeForName:@"name"] stringValue] forKey:@"name"];
        [monthItem setObject:[[resultElement attributeForName:@"month"] stringValue] forKey:@"month"];
        [monthItem setObject:[[resultElement attributeForName:@"year"] stringValue] forKey:@"year"];

        Month *aMonth = [NSEntityDescription insertNewObjectForEntityForName:@"Month" inManagedObjectContext:_managedObjectContext];
        aMonth.name =  [monthItem objectForKey:@"name"];
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        aMonth.month = [f numberFromString:[monthItem objectForKey:@"month"]];
        aMonth.year = [f numberFromString:[monthItem objectForKey:@"year"]];

        NSArray *itemNodes = [resultElement children];

        for (CXMLElement *resultItem in itemNodes) {
            NSMutableDictionary *item = [[NSMutableDictionary alloc] init];

            [item setObject:[[resultItem attributeForName:@"id"] stringValue] forKey:@"id"];

            int counter;
            for(counter = 0; counter < [resultItem childCount]; counter++) {
                [item setObject:[[resultItem childAtIndex:counter] stringValue] forKey:[[resultItem childAtIndex:counter] name]];

            }

            Item *aItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:_managedObjectContext];
            NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
            aItem.nr = [f numberFromString:[item objectForKey:@"id"]];
            aItem.title = [item objectForKey:@"title"];
            aItem.body = [item objectForKey:@"body"];
            aItem.date = [item objectForKey:@"date"];
            aItem.start = [item objectForKey:@"start"];
            aItem.end = [item objectForKey:@"end"];
        aItem.start2 = [item objectForKey:@"start2"];
            aItem.end2 = [item objectForKey:@"end2"];
            aItem.link_url = [item objectForKey:@"link_url"];
            aItem.month = aMonth;
            [aMonth addItemObject:aItem];
        }
    }
}

比較する必要があるのは項目の日付です。

これは私のテーブルビュー cellForRowAtIndex コードの一部です:

Month *aMonth = [_stages objectAtIndex:indexPath.section];
    NSSortDescriptor *sortDescriptorItem = [NSSortDescriptor sortDescriptorWithKey:@"nr" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects: sortDescriptorItem, nil];
    _stagesPerMonth = [[aMonth.item allObjects] sortedArrayUsingDescriptors:sortDescriptors];
    Item *aItem = [_stagesPerMonth objectAtIndex:indexPath.row];
4

1 に答える 1

0

日付を表示するためのラベルを持つカスタムセルを使用していると思います。の疑似コードは次のcellForRowAtIndexようになります。

Item *aItem = [_stagesPerMonth objectAtIndex:indexPath.row];
NSDate thisDate = aItem.date;

// Obtain the date of the previous event or nil if this is the first event
NSDate prevDate = indexPath.row > 0 ? _stagesPeMonth[indexPath.row - 1].date : nil;

// Date label is hidden if the previous date is the same as this.
dateLabel.hidden = [thisDate isEqualToDate: prevDate];

あなたの場合、構文は異なる場合があります。

于 2013-03-10T11:52:53.033 に答える