将 NSarray 拆分为多个部分时出错
好吧,我一直在研究一个与我想要实现的目标非常匹配的示例,唯一的区别是在该示例中,他直接从数据库中调用他需要分段的数据等。 NSArray。
这是我正在研究的教程 - iPhone 开发:创建Native Contacts like screen
我创建了一个方法,它捕获 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 部分。然后仅将这些值加载到每个部分,而不是每个部分部分。
Okay so I have been working through an example that closely matches what I am trying to achive, the sole difference being that in the example he is directly calling from his database the data he needs to be sectioned etc. Where as I already have a sorted NSArray.
This is the tutorial I am working off - iPhone Development: Creating Native Contacts like screen
I have created a Method that is capturing each entry in the NSArray and putting these results into a alpha based NSDictionary (so their will be a NSDictionary for A,B,C... etc)
here is my method.
//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];
}
}
This is putting every value into every alpha section, I am hoping someone can help me with making it so that only alpha sections are established if there is a value in the array for it.. then only loading those values into each section, not every section.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码就可以做到这一点,并且比为每个字母过滤一次数组要高效得多。
请注意,arraysByLetter 是一个字典,其中初始数据中存在的每个“第一个字母”包含一个数组。
--- 于 2011-09-23 添加 ---
输出如下:
This piece of code will do just that and will be much more efficient than filtering the array once for each letter.
Note that arraysByLetter is a dictionary that contains one array per "first letter" that exists in your initial data.
--- Added on 2011-09-23 ---
The output is the following:
看起来您需要使用每个字母的谓词来过滤
sortedArray
。像这样的东西...Looks like you need to filter
sortedArray
with a predicate for each letter. Something like this...