错误将NSarray分割成部分
好吧,所以我一直在通过一个与我正在尝试实现的密切匹配的示例,唯一的区别是,在他直接从数据库中调用他需要分段的数据等例子中,因为我已经有一个sortingNSArray的。
这是我正在工作的教程 – iPhone开发:创build本地联系人,如屏幕
我已经创build了一个方法,它捕获NSArray中的每个条目,并将这些结果放入一个基于alpha的NSDictionary(所以它们将成为A,B,C等的NSDictionary)
这是我的方法。
//method to sort array and split for use with uitableview Index - (IBAction)startSortingTheArray:(NSMutableArray *)arrayData { //Sort incoming array alphabetically //sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; [self setSortedArray:[arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]]; arrayOfCharacters = [[NSMutableArray alloc]init]; objectsForCharacters = [[NSMutableDictionary alloc]init]; for(char c='A';c<='Z';c++) { if([sortedArray count] >0) { [arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]]; [objectsForCharacters setObject:sortedArray forKey:[NSString stringWithFormat:@"%c",c]]; NSLog(@"%@", objectsForCharacters); } [sortedArray release]; //Reloads data in table [self.tableView reloadData]; } }
这是把每个值放入每个alpha部分,我希望有人可以帮助我,使得只有alpha部分build立,如果有一个值在数组中,然后只加载这些值到每个部分,而不是每部分。
这段代码可以做到这一点,并且比为每个字母过滤一次数组效率高得多。
//Sort incoming array alphabetically so that each sub-array will also be sorted. NSArray *sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; // Dictionary will hold our sub-arrays NSMutableDictionary *arraysByLetter = [NSMutableDictionary dictionary]; // Iterate over all the values in our sorted array for (NSString *value in sortedArray) { // Get the first letter and its associated array from the dictionary. // If the dictionary does not exist create one and associate it with the letter. NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)]; NSMutableArray *arrayForLetter = [arraysByLetter objectForKey:firstLetter]; if (arrayForLetter == nil) { arrayForLetter = [NSMutableArray array]; [arraysByLetter setObject:arrayForLetter forKey:firstLetter]; } // Add the value to the array for this letter [arrayForLetter addObject:value]; } // arraysByLetter will contain the result you expect NSLog(@"Dictionary: %@", arraysByLetter);
请注意,arraysByLetter是一个字典,它包含一个数组,每个“首字母”存在于您的初始数据中。
—添加于2011-09-23 —
[sortedArray removeAllObjects]; NSArray *sortedKeys = [arraysByLetter.allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; for (NSString *key in sortedKeys) { [sortedArray addObject:key]; [sortedArray addObjectsFromArray: [arraysByLetter objectForKey:key]]; } NSLog(@"Sorted Array: %@", sortedArray);
输出如下:
C, Computer, H, Helene, Hello, J, Jules, W, World
看起来像你需要过滤sortedArray
与每个字母的谓词。 像这样的东西…
for(char c='A';c<='Z';c++) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] '%c'", c]; NSArray *objectsBeginningWithCurrentLetter = [array filteredArrayUsingPredicate:predicate]; if([sortedArray count] >0) { [arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]]; if ([objectsBeginningWithCurrentLetter count] > 0) { [objectsForCharacters setObject:objectsBeginningWithCurrentLetter forKey:[NSString stringWithFormat:@"%c",c]]; NSLog(@"%@", objectsForCharacters); } } [sortedArray release]; //Reloads data in table [self.tableView reloadData]; }
- 使用UIPageViewController和UISegmentedControl进行平滑过渡,就像在Apple Music App中一样
- 导航栏中的ios标题和副标题居中
- Swift World:设计模式-观察者
- objective-c:如何将string分配给NSMutableData?
- 不是PNG filCommand copypng发出错误,但没有返回非零退出代码来指示失败
- 有没有办法使用worklight 6.2或phonegap混合应用程序从Android和iOS的另一个应用程序启动一个应用程序?
- iOS:使用insertRowsAtIndexPaths在UITableview中插入多行
- 如何渲染一个旋转的UIlabel
- 传递一个Swift类作为参数,然后调用一个类方法