0

こんにちは、テーブル ビューに並べ替え機能を実装したアプリケーションの 1 つを作成しました。並べ替え方法は iOS 4 および 5 では正常に動作していますが、iOS 6 でアプリケーションをテストしようとすると、並べ替え方法にエラーが表示されます。 iOS 6 助けてくださいここに画像の説明を入力

方法 :-

-(void)setupIndexData{
    self.arrayOfCharacters =[[NSMutableArray alloc]init];
    self.objectForCharacter=[[NSMutableDictionary alloc]init];

    NSNumberFormatter *formatter =[[NSNumberFormatter alloc]init];
    NSMutableArray *arrayOfNames =[[NSMutableArray alloc]init];
    NSString *numbericSection    = @"#";
    NSString *firstLetter;
    for (NSDictionary *item in self.mCompanyarray) {

        firstLetter = [[[item valueForKey:@"Company"]description] substringToIndex:1];

        // Check if it's NOT a number
        if ([formatter numberFromString:firstLetter] == nil) {

            /**
             * If the letter doesn't exist in the dictionary go ahead and add it the 
             * dictionary.
             *
             * ::IMPORTANT::
             * You HAVE to removeAllObjects from the arrayOfNames or you will have an N + 1
             * problem.  Let's say that start with the A's, well once you hit the 
             * B's then in your table you will the A's and B's for the B's section.  Once 
             * you hit the C's you will all the A's, B's, and C's, etc.
             */
            if (![objectForCharacter objectForKey:firstLetter]) {

                [arrayOfNames removeAllObjects];

                [arrayOfCharacters addObject:firstLetter];
            } 

            [arrayOfNames addObject:item];

            /**
             * Need to autorelease the copy to preven potential leak.  Even though the 
             * arrayOfNames is released below it still has a retain count of +1
             */
            [objectForCharacter setObject:[[arrayOfNames copy] autorelease] forKey:firstLetter];

        } else {

            if (![objectForCharacter objectForKey:numbericSection]) {

                [arrayOfNames removeAllObjects];

                [arrayOfCharacters addObject:numbericSection];
            }

            [arrayOfNames addObject:item];

            [objectForCharacter setObject:[[arrayOfNames copy] autorelease] forKey:numbericSection];
        }
    }

    [formatter release];
    [arrayOfNames release];  

    [self.mCompaniesTableView reloadData];
}

ありがとう

4

1 に答える 1

2

UILocalizedIndexedCollat​​ionを使用して、データの並べ替えとインデックス付けを行います。そうすれば、アプリは複数の言語などをサポートできます。

注:以下のコードはテストしていませんが、理論はあります。

まず、@ propertyを作成して、インデックス付きデータを保存します。

@property (nonatomic, strong) NSDictionary *indexedSections;

次のようにテーブルを設定します。

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //we use sectionTitles and not sections
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.indexedSections objectForKey:[NSNumber numberWithInteger:section]] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    BOOL showSection = [[self.indexedSections objectForKey:[NSNumber numberWithInteger:section] count] != 0;

    //only show the section title if there are rows in the section
    return (showSection) ? [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section] : nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = [[self.indexedSections objectForKey:[NSNumber numberWithInteger:indexPath.section]] objectAtIndex:indexPath.row];

    // configure the cell
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //sectionForSectionIndexTitleAtIndex: is a bit buggy, but is still useable
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}

そして最後に次のようにインデックスを作成します。

- (void) setupIndexData
{
    // asynchronously sort
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
    {
        // create a dictionary to store an array of objects for each section
        NSMutableDictionary *tempSections = [NSMutableDictionary dictionary];

        // iterate through each dictionaey in the list, and put them into the correct section
        for (NSDictionary *item in self.mCompanyarray)
        {
            // get the index of the section (Assuming the table index is showing A-#)
            NSInteger indexName = [[UILocalizedIndexedCollation currentCollation] sectionForObject:[item valueForKey:@"Company"] collationStringSelector:@selector(description)];

            NSNumber *keyName = [NSNumber numberWithInteger:indexName];

            // if an array doesnt exist for the key, create one
            NSMutableArray *arrayName = [tempSections objectForKey:keyName];
            if (arrayName == nil)
            {
                arrayName = [NSMutableArray array];
            }

            // add the dictionary to the array (add the actual value as we need this object to sort the array later)
            [arrayName addObject:[item valueForKey:@"Company"]];

            // put the array with new object in, back into the dictionary for the correct key
            [tempSections setObject:arrayName forKey:keyName];
        }

        /* now to do the sorting of each index */

        NSMutableDictionary *sortedSections = [NSMutableDictionary dictionary];

        // sort each index array (A..Z)
        [tempSections enumerateKeysAndObjectsUsingBlock:^(id key, id array, BOOL *stop)
         {
             // sort the array - again, we need to tell it which selctor to sort each object by
             NSArray *sortedArray = [[UILocalizedIndexedCollation currentCollation] sortedArrayFromArray:array collationStringSelector:@selector(description)];
             [sortedSections setObject:[NSMutableArray arrayWithArray:sortedArray] forKey:key];
         }];

        // set the global sectioned dictionary
        self.indexedSections = sortedSections;

        dispatch_async(dispatch_get_main_queue() ,^{
          // reload the table view (on the main thread)
          [self.mCompaniesTableView reloadData];
        });
    });
}
于 2012-10-11T09:56:36.183 に答える