gpt4 book ai didi

iphone - 在 iOS 6 的 TableView 中对数组进行排序

转载 作者:行者123 更新时间:2023-11-28 22:47:28 30 4
gpt4 key购买 nike

您好,我创建了一个应用程序,其中我在 TableView 中实现了排序功能排序方法在 iOS 4 和 5 上运行良好,但是当我尝试在 iOS 6 上测试该应用程序时,它显示错误iOS 6 上的排序方法请帮忙enter image description here

方法:-

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

谢谢

最佳答案

我会使用 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];
});
});
}

关于iphone - 在 iOS 6 的 TableView 中对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834033/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com