来自表格部分和内容的 NSDictionary

发布于 2024-11-18 00:12:32 字数 1244 浏览 5 评论 0原文

编辑:更新代码

我正在尝试从表部分创建字典,然后创建每个部分中的索引列表,因此部分字母将是关键...对于我拥有的部分:

self.collation = [UILocalizedIndexedCollation currentCollation];

NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];

NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

NSString *alphabet = [[NSString alloc] initWithString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];

for (index = 0; index < sectionTitlesCount; index++) {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [newSectionsArray addObject:array];

    NSString *letter = [alphabet substringWithRange:NSMakeRange(index, 1)];
    [myDict setObject:newSectionsArray forKey:letter];

    [array release];
}

这会崩溃与:

'*** -[NSCFString substringWithRange:]: Range or index out of bounds'

然后对于每个字母,我需要添加该部分的数据,我认为该数组将是部分/字母键的对象:

for (Data *myData in self.dataModel.datalist) {

    NSInteger sectionNumber = [collation sectionForObject:myData collationStringSelector:@selector(myName)];

    NSMutableArray *sectionData = [newSectionsArray objectAtIndex:sectionNumber];

    [sectionData addObject:myData];

}

希望这是有道理的!谢谢!!!

Edit: Updated code

I'm trying to create a dictionary from table sections and then the index list within each section, so the section letter would be the key... For the sections I have:

self.collation = [UILocalizedIndexedCollation currentCollation];

NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];

NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

NSString *alphabet = [[NSString alloc] initWithString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];

for (index = 0; index < sectionTitlesCount; index++) {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [newSectionsArray addObject:array];

    NSString *letter = [alphabet substringWithRange:NSMakeRange(index, 1)];
    [myDict setObject:newSectionsArray forKey:letter];

    [array release];
}

This crashes with:

'*** -[NSCFString substringWithRange:]: Range or index out of bounds'

Then for each letter I need to add the data for that section, I think this array would be the object for the section/letter key:

for (Data *myData in self.dataModel.datalist) {

    NSInteger sectionNumber = [collation sectionForObject:myData collationStringSelector:@selector(myName)];

    NSMutableArray *sectionData = [newSectionsArray objectAtIndex:sectionNumber];

    [sectionData addObject:myData];

}

Hope this makes sense! Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把时间冻结 2024-11-25 00:12:33

在数组中定义您的部分

        sections = [NSArray arrayWithObjects:@"#", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", nil];

并实现委托方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView {
    return [sections count];
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return sections;
}

然后在 - (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 中您可以获取数据的子部分

NSArray *sectionArray = [myData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:[indexPath section]]]];

Define your sections in an array

        sections = [NSArray arrayWithObjects:@"#", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", nil];

implement the delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView {
    return [sections count];
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return sections;
}

Then in - (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath { you can get your subsection of the data

NSArray *sectionArray = [myData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:[indexPath section]]]];
嗼ふ静 2024-11-25 00:12:33

关于崩溃,这表明 sectionTitlesCount 的值大于字符串 alphabet 中的字符数。

我不确定 sectionTitlesCount 代表什么值或它来自哪里(因为您的代码中缺少),但最好替换第一行你的for循环

for (index = 0; index < alphabet.length; index++) {

With regards the crash, it's suggesting that the value of sectionTitlesCount is greater than the number of characters in the string alphabet.

I'm not sure what value sectionTitlesCount represents or where it comes from (as this is absent from your code), but it'd be better to replace the first line of your for loop with

for (index = 0; index < alphabet.length; index++) {
入画浅相思 2024-11-25 00:12:33

发布您的sectionTitlesCount 值。它可能大于字符串的长度。

Post your sectionTitlesCount value. It might be greator than the length of string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文