1

我正忙于在 TableView 中显示事件列表的应用程序。每行/单元格我有一个事件显示事件的标题和日期。现在的问题是,当一天有 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];
        }
    }
}

这是需要比较的项目日期。

这是我的 tableview 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 回答