2011-09-19 3 views
2

이 문제로 고생했습니다. 저는 UITableView를 가지고 NSMutableArray의 데이터로 채 웁니다. 배열의 구조는 다음과 같습니다.UITableView는 NSMutableArray에서 매일 1 섹션을 만듭니다.

<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 '메서드에서 행에 올바른 값을 제공하는 데 도움이 될 수 있습니다.

+0

단지 측 의견 만 g 모든 시간을 누출의 당신 이 방법을 호출하십시오 –

+0

ARC를 사용하지 않을 것입니다;) – JonasG

답변

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

위의 코드에서 allocinit g 및 s는 필요하지 않으며 메모리 누수의 원인이됩니다.

나는 배열을 통과하여 사전을 포함하는 두 번째 배열을 만들 것이다. 각 사전에는 날짜와 배열 하나의 두 객체가 있습니다. 배열은 각 날짜에 적용 할 수있는 모든 값을 보유합니다.

이 기술을 사용하면 주 배열의 개수는 섹션의 개수가되고 섹션 당 행은 사전 내에 보관 된 배열의 개수가됩니다. 각 행의 값은 섹션 (주 배열) 및 행 (내부 배열)에 대한 적절한 색인에있는 객체입니다. 여기

당신이 원하는 것을 할 수있는 몇 가지 아주 약간 테스트 코드입니다 : 이것은 각각의 날짜에 대한 사전 및 모든 값의 배열을 원하는 분야 한 배열, datesList, 당신을 떠나야한다

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

각 날짜.

+0

좋은 생각, 아픈 시도 내일 :) – JonasG

+0

그걸 도와 주실 수 있습니까? 제대로 작동하지 않습니다! – JonasG

+0

가지고 계신 내용으로 질문을 업데이트하십시오. 최선을 다할 것입니다. – jrturton

관련 문제