iossqliteuitableviewuilabeltouchxml

iOS How to hide double values in UITableView row


I am busy with an application where I show a list of events in a TableView. Per row/cell I have an event showing the title of the event and the date. The problem now is that when there are 2 or more events on one day the app now shows them nicely per cell but the user will see the date 2 or more times repeatedly, which looks inefficient and kinda redundant. So my thought was to check wether the previous cell date is similar to the current cell date and this would make the current cell date hidden (I cannot remove it completely, cause the user is able to save the event into the calendar).

I am reading an external XML into a SQLite database via TouchXML.

Here is a sample of my code:

-(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];
        }
    }
}

It is the item date that needs to be compared.

This is part of my tableview cellForRowAtIndex code:

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];

Solution

  • I suppose you use a custom cell that has a label for displaying the date. A pseudo code for cellForRowAtIndex would be:

    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];
    

    The syntax may be different in your case.