2012-01-10 97 views
-1

我有一個分組的uitableview有幾個部分。這些部分顯示正常,行數正確,但列表項的位置有問題。我有一個名字列表的NSMutableArray,然後我使用subArrayWithRange分割這個數組。請參閱代碼以獲取更多詳細信息。問題是列表項目沒有出現在正確的部分,我似乎無法找到解決方案。iPhone SDK:分組的UITableView部分,列表項不顯示在正確的部分

在的cellForRowAtIndexPath:

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

下面的答案能幫助你嗎?你是否仍然有這個問題需要解決? – 2012-01-14 12:04:28

回答

1

我覺得你是過於複雜的事情。爲什麼不帶你,然後做這樣的事情在的cellForRowAtIndexPath數據創建段(只用一節標題的詞典):

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

... 
} 

下面是我用用於構建字典的方法:

-(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

好的,謝謝你會試試這個! – 2012-01-10 11:35:04

+0

請注意,單位NSMutableDictionary是我的ViewController中的一個屬性。 – 2012-01-10 11:41:05

+0

目前節標題是在鎮陣列,所以我創建一個新的字典與節標題,然後將其添加到nsmutablearray? – 2012-01-10 11:49:03