2012-01-10 7 views
-1

私はいくつかのセクションを持つグループ化されたuitableviewを持っています。セクションが正常に表示され、行数が正しく表示されますが、リスト項目の配置に問題があります。私は名前のリストを持つNSMutableArrayを持っています。次に、この配列をsubArrayWithRangeを使って分割します。詳細はコードを参照してください。問題は、リスト項目が正しいセクションの下に表示されないことです。解決策が見つからないようです。 cellForRowAtIndexPathでiPhone SDK:グループ化されたUITableViewセクション、リスト項目が正しいセクションに表示されない

NSInteger section = [indexPath section]; 
    NSString *sectionName = [sectionNamesArray objectAtIndex:section]; 
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:townArray]; 
    int numOfRows = [set countForObject:[NSString stringWithFormat:@"%@",sectionName]]; 
    int initNumRows = [set countForObject:[NSString stringWithFormat:@"%@",[sectionNamesArray objectAtIndex:0]]]; 

if(section == 0) { 

    NSRange range = NSMakeRange(0,numOfRows); 

    NSArray *subArray = [[namesArray subarrayWithRange:range] retain]; 
     NSArray *subTownArray = [[townArray subarrayWithRange:range]retain]; 
     //indexTracker = indexPath.row; 

    //NSLog(@"sub array === %@, index Tracker = %i",subArray,indexTracker); 
     cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row]; 
     cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row]; 
    } else if (section == [sectionNamesArray count]-1) { 

     int previousSection = section - 1; 
     int previousNumRows = [set countForObject:[NSString stringWithFormat:[sectionNamesArray objectAtIndex:previousSection]]]; 
     NSRange range = NSMakeRange(previousNumRows+initNumRows,numOfRows); 
     NSLog(@"previous num rows = %i and current num of rows = %i",previousNumRows,numOfRows); 
     NSArray *subArray = [[namesArray subarrayWithRange:range] retain]; 
     NSArray *subTownArray = [[townArray subarrayWithRange:range]retain]; 
     cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row]; 
     cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row]; 
     NSLog(@"sub array === %@",subArray); 


    } else { 

     int previousSection = section - 1; 


     int previousNumRows = [set countForObject:[NSString stringWithFormat:[sectionNamesArray objectAtIndex:previousSection]]]; 
     int location = previousNumRows; 
     NSRange range = NSMakeRange(previousNumRows,numOfRows); 
     NSLog(@"location = %i",location); 
     NSLog(@"previous num rows = %i and current num of rows = %i",previousNumRows,numOfRows); 
     NSArray *subArray = [[namesArray subarrayWithRange:range] retain]; 
     NSArray *subTownArray = [[townArray subarrayWithRange:range]retain]; 
     cell.primaryLabel.text = [subArray objectAtIndex:indexPath.row]; 
     cell.secondaryLabel.text = [subTownArray objectAtIndex:indexPath.row]; 

    }  //NSLog(@"section name = %@ . num of rows = %i",sectionName,numOfRows); 





    return cell; 
+0

以下の回答はあなたを助けてくれましたか?あなたはそれを働かせるのにまだ問題がありますか? –

答えて

1

私はあなたが上に複雑なものだと思います。

:ここ

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *selRow = [[self.units valueForKey:[[[self.units allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 

... 
} 

私はその辞書を構築するための使用方法をされて:なぜあなたは持っているし、その後cellForRowAtIndexPathでこのような何かを行うデータであなたのセクション(セクションだけのタイトルと辞書)を作成しません

-(void)buildSectionKeysWithMutableArray:(NSMutableArray *)mutableArray { 
    NSMutableString *prevUnit; 
    units = [[NSMutableDictionary alloc] init]; 

    // Loop through the mutableArray passed in and build our keys 
    for (NSDictionary *thisUnit in mutableArray) 
    { 
     NSString *unitName = [thisUnit objectForKey:@"LOCATION"]; 
     if (![prevUnit isEqualToString:unitName]) { 
      [self.units setValue:[[NSMutableArray alloc] init] forKey:unitName];     
     } 
     prevUnit = [NSString stringWithString:unitName]; 
    } 

    // Loop again and add the keys into the units mutableDictionary 
    for (NSDictionary *thisUnit in mutableArray) 
    { 
     [[self.units objectForKey:[thisUnit objectForKey:@"LOCATION"]] addObject:thisUnit]; 
    } 

} 

これにより、データの表示をダイナミックに保つことができます。

+0

ありがとうございます! –

+0

NSMutableDictionaryのユニットは、私のViewControllerのプロパティです。 –

+0

セクションのタイトルはtownArrayにありますので、セクションタイトルの新しい辞書を作成し、これをnsmutablearrayに追加しますか? –

関連する問題