来自表格部分和内容的 NSDictionary
编辑:更新代码
我正在尝试从表部分创建字典,然后创建每个部分中的索引列表,因此部分字母将是关键...对于我拥有的部分:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在数组中定义您的部分
并实现委托方法
然后在
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
中您可以获取数据的子部分Define your sections in an array
implement the delegate methods
Then in
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
you can get your subsection of the data关于崩溃,这表明 sectionTitlesCount 的值大于字符串 alphabet 中的字符数。
我不确定 sectionTitlesCount 代表什么值或它来自哪里(因为您的代码中缺少),但最好替换第一行你的for循环与
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
发布您的sectionTitlesCount 值。它可能大于字符串的长度。
Post your sectionTitlesCount value. It might be greator than the length of string.