由于未捕获的异常"NSRangeException"而终止应用程序,原因:IOS 中'*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    data_web *current_data ;
    current_data = [[structured_question data_webs] objectAtIndex:indexPath.row];
    NSString *is_submited = [NSString stringWithFormat:@"%@",[current_data content_2]];
    if ([is_submited compare:@"Y"] == NSOrderedSame)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

}

以上是我的脚本。 我的脚本有问题,我找不到解决方案。

在注释中,您提供了numberOfRowsInSection方法的代码,如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return [[testM data_webs] count];
    else
        return [[testS data_webs] count];
}

但是,当您访问current_data时,您的willDisplayCell会忽略indexPath.section。当显示具有更多行的部分中的单元格时,您的代码将崩溃。

您应该将代码更改为如下所示的内容:

NSArray *data_webs;
// This part mimics your "numberOfRowsInSection'
if (indexPath.section == 0) {
    data_webs = [testM data_webs];
} else {
    data_webs = [testS data_webs];
}
data_web *current_data ;
current_data = [data_webs objectAtIndex:indexPath.row];

我不知道您的代码中structured_question是什么,以及您为什么要使用它,但以上应该消除 objectAtIndex: 方法中的崩溃。

最新更新