ios - 我在 UITableView 中实现了部分索引,当我点击该部分时它会抛出一个错误

标签 ios objective-c uitableview

我从 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/

相关文章:

ios - 如何在 UiTableViewCell 中动态更改 ImageView?

iOS - UITableViewCell,高亮时设置 subview 背景图片

ios - 尝试使用约束以编程方式移动 UIPickerView 但总是收到此错误消息

ios - 从自定义单元格 UITableView 中删除行

ios - 检测点击 UITableViewCell 中的 UIViewCollectionCell

ios - UITableViewRowAction 在按下时更改背景颜色

ios - 如何使我的按钮在抽屉导航中居中并使用 swift 删除边缘阴影?

iphone - 如何检测 iPhone 屏幕是否锁定(关闭)

ios - UITableView 顶部的 UISegementController

ios - 未调用 UITableview 委托(delegate)