UITableView每天从NSMutableArray创建1个部分

时间:2011-09-19 20:04:45

标签: objective-c ios cocoa-touch uitableview

我遇到了这个问题,我有一个UITableView,我用NSMutableArray中的数据填充它。 Array具有以下结构:

<array>
<dict>
    <key>Date</key>
    <string>2011.18.09 04:17 PM</string>
    <key>Value</key>
    <string>58.00</string>
</dict>
<dict>
    <string>2011.15.09 04:21 PM</string>
    <key>Value</key>
    <string>0.00</string>
</dict>
</array>

我想在数组中每天都有一个部分。我已经弄清楚如何返回日期数,并命名节标题,我唯一的问题是返回每个节的行数,然后定义每行的单元格。

工作代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"];
NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path];
NSMutableArray *test = [d valueForKey:@"Date"];
NSString *g = [[NSString alloc] init];
NSString *s = [[NSString alloc] init];
c = 0;
    for (g in test) {
        s = [g substringToIndex:[g length]-9];
        if (![s isEqualToString:sf]) {
            c = c+1;
            [dates addObject:s];
        }
        sf = [g substringToIndex:[g length]-9];
    }
return c;

该代码计算不同的日期并创建一个包含节名称的数组。任何人都可以帮我定义行数(通过计算每天的对象数),并通过在'cellForRowAtIndexPath'方法中为行提供正确的值。

2 个答案:

答案 0 :(得分:0)

上面的代码中不要allocinit g和s,这不是必需的,并且会导致内存泄漏。

我会通过你的数组并创建一个包含字典的第二个数组。每个字典都包含两个对象,一个日期和一个数组。该数组将包含适用于每个日期的所有值。

使用这种技术,主数组的计数将是部分的数量,每个部分的行数将是字典中保存的数组的计数。每行的值将是段(主数组)和行(内部数组)的适当索引处的对象。

以下是一些经过精心测试的代码,可以执行您想要的操作:

    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:@"test.plist"];
    [array sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:YES]]];

    NSMutableArray *datesList = [[NSMutableArray alloc] init ];
    NSString *previousDate = @"";
    NSMutableArray *currentDateValues;

    for (NSDictionary * dict in array)
    {
        if ([[[dict objectForKey:@"Date"] substringToIndex:10] isEqualToString:previousDate])
        {
            // This is another entry from the same date
            [currentDateValues addObject:[dict objectForKey:@"Value"]];
        }
        else
        {
            // We haven't seen this date before
            previousDate = [[dict objectForKey:@"Date"] substringToIndex:10];
            currentDateValues = [NSMutableArray array];
            [currentDateValues addObject:[dict objectForKey:@"Value"]];
            NSDictionary *newDate = [NSDictionary dictionaryWithObjectsAndKeys:previousDate,@"Date",currentDateValues,@"Values", nil];
            [datesList addObject:newDate];
        }
    }

这应该为您留下一个数组datesList,它包含每个日期的字典和每个日期的所有值的数组。

答案 1 :(得分:0)

好的,我终于找到了解决方案。实际上很容易理解。要返回部分的数量,我需要找出数组中有多少个不同的日期。对于行数我需要知道每天有多少值。所以这就是我所做的:

行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"];
    NSMutableArray *d = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSArray *jes = [[NSArray alloc] init];
    count = [[NSMutableArray alloc] init];
    NSString *before = [[NSString alloc] init];
    NSString *after = [[NSString alloc] init];
    jes = [d valueForKey:@"Date"];
    int ja = -1;
    for (int i = 1; i<=[jes count];i++) {
        ja = ja+1;
        before = [jes objectAtIndex:ja];
        if (![before isEqualToString:after]) {
            NSCountedSet *ca = [[NSCountedSet alloc] initWithArray:jes];
            int f = [ca countForObject:before];
            [count addObject:[NSString stringWithFormat:@"%i",f]];
        }
        after = before;
    }
    return [[count objectAtIndex:section] intValue];
}

节数:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"];
    NSMutableArray *d = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSArray *jes = [[NSArray alloc] init];
    count = [[NSMutableArray alloc] init];
    NSString *before = [[NSString alloc] init];
    NSString *after = [[NSString alloc] init];
    jes = [d valueForKey:@"Date"];
    int ja = -1;
    for (int i = 1; i<=[jes count];i++) {
        ja = ja+1;
        before = [jes objectAtIndex:ja];
        if (![before isEqualToString:after]) {
            NSCountedSet *ca = [[NSCountedSet alloc] initWithArray:jes];
            int f = [ca countForObject:before];
            [count addObject:[NSString stringWithFormat:@"%i",f]];
        }
        after = before;
    }
    return [count count];
}

标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"];
    NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path];
    NSMutableArray *test = [d valueForKey:@"Date"];
    NSString *g = [[NSString alloc] init];
    NSString *s = [[NSString alloc] init];
    NSString *sf = [[NSString alloc] init];
    dates = [[NSMutableArray alloc] init];
    c = 0;
    for (g in test) {
        s = [g substringToIndex:[g length]-9];
        if (![s isEqualToString:sf]) {
            c = c+1;
            [dates addObject:s];
        }
        sf = [g substringToIndex:[g length]-9];
    }
    return [dates objectAtIndex:section];
}