我从 JSON
获取数组。我已经在 UITableView
中实现了部分索引,当我点击该部分时,它会抛出一个错误。
注意:传递数组来自 JSON,字母数组是 abcd 等。我收到的错误是 [__NSDictionaryM substringToIndex:]: 无法识别的选择器发送到实例
。
请帮帮我。我正在从联系人中检索此信息并在 JSON 中传递这些联系人:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return alphabetsArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([passingArray count] > 0) {
return [passingArray count];
}
else
return 1;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabetsArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
for (int i = 0; i < [passingArray count]; i++) {
NSString *letterString = [[passingArray objectAtIndex:i] substringToIndex:1];
if ([letterString isEqualToString:title]) {
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
break;
}
}
return -1;
}
- (void)CreateIndexArray {
NSMutableArray *tempFirstLetterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [passingArray count]; i++) {
NSString *letterString = [[passingArray objectAtIndex:i] substringToIndex:1];
if (![tempFirstLetterArray containsObject:letterString]) {
[tempFirstLetterArray addObject:letterString];
}
}
passingArray = tempFirstLetterArray;
}
最佳答案
sectionForSectionIndexTitle:
的逻辑有问题。该方法需要一个整数返回值 - tableview 应该向下滚动到的索引。
passingArray
也是字典数组吗?这就是错误提示的内容
[passingArray objectAtIndex:i]
如果您希望将它与 substringToIndex:
一起使用,则应返回一个字符串,而不是字典。如果该对象是字典,请使用您拥有的任何键从其中获取正确的值,然后对该值运行字符串操作。
关于ios - 我在 UITableView 中实现了部分索引,当我点击该部分时它会抛出一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39525868/