Ios “禁止入内”;NSRANGE例外情况“;在UITableView上滚动时

Ios “禁止入内”;NSRANGE例外情况“;在UITableView上滚动时,ios,objective-c,uitableview,ios7,Ios,Objective C,Uitableview,Ios7,我正在构建一个简单的目录应用程序,它扫描Amazon Dynamo DB数据库,并以树状结构显示数据。数据库中的某些项只有子项,而其他项除了子项之外还包含地址和电话号码详细信息。详细信息项在数据库中没有自己的条目——它们只是在另一个项中有一个字段,这将大大减少数据库的大小 当我的UITableView必须同时显示详细信息和子项时,它会变得有点棘手。细节加载在屏幕顶部,每次加载细节时,我都会增加一个_arrayOffset。从indexPath.row中减去_arrayOffset,以便从索引[0

我正在构建一个简单的目录应用程序,它扫描Amazon Dynamo DB数据库,并以树状结构显示数据。数据库中的某些项只有子项,而其他项除了子项之外还包含地址和电话号码详细信息。详细信息项在数据库中没有自己的条目——它们只是在另一个项中有一个字段,这将大大减少数据库的大小

当我的UITableView必须同时显示详细信息和子项时,它会变得有点棘手。细节加载在屏幕顶部,每次加载细节时,我都会增加一个_arrayOffset。从indexPath.row中减去_arrayOffset,以便从索引[0]开始用子数组正确填充表。当列表可以在不滚动的情况下显示时,这种方法非常有效。但是,如果向下滚动查看详细信息并进行备份,则会立即出现nsrange异常。在我所看到的特定案例中,我有22个孩子要显示,还有1个细节。错误显示:

Terminating app due to uncaught exception 'NSRangeException', reason '***
-[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 22]'
我知道4294967295是(2^32)-1,所以我非常确定的是,当我的详细信息重新加载到表的顶部时,它会再次增加我的_arrayOffset,然后UITableView会尝试使用索引-1处的项

我怎样才能避免这种情况?是否有办法仅在首次加载详细信息时增加_arrayOffset?我可以增加UITableView一次加载的单元格数量吗?如果我必须一次加载50个单元格,它会不会太慢?如果我的无代码问题不够具体,无法回答这些问题,有人能帮助我理解UITableView如何重用单元格,以便我更好地调试它吗

编辑1:

索引由cellForRowAtIndexPath生成:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
然后

然后

// If _tableRows isn't empty
if (_tableRows.count)
{
    DDBTableRow *item = self.tableRows[indexPath.row-self.arrayOffset];
    cell.textLabel.text = item.title;
    cell.detailTextLabel.text = nil;
 }
编辑2:

我不相信numberOfRowsInSection是罪魁祸首,但无论如何我都会发布它。 行数始终是正确的,即使在崩溃时也是如此

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // If we're displaying details, check to see if the item has a phone number, fax, and address.
    // Add one to rowCount for phone number and fax. For address, add two, so we can display a dummy
    // cell at the end. If we don't do this, the size of every cell will be the size of the address
    // cell (big) and that looks messy.

    NSInteger rowCount = 0;

    if (self.showDetails == YES)
    {
        if ([_tableRow.phone length] > 0)
        {
            rowCount++;
        }
        if ([_tableRow.fax length] > 0)
        {
            rowCount++;
        }
        if ([_tableRow.address length] > 0)
        {
            rowCount++;
            rowCount++;
        }
    }

    rowCount += [self.tableRows count];

    return rowCount;
}

numberOfRowsInSection函数在哪里?在以下位置放置断点:
DDBTableRow*item=self.tableRows[indexPath.row self.arrayOffset]
并查看在崩溃之前self.arrayOffset是什么。在崩溃之前,self.arrayOffset是1。indexPath.row是0,对吗?然后是索引-1,这是不可能的。当UITableView第一次加载时,第一个非细节项的indexath.row是1,所以我必须减去self.arrayOffset。然而,当我向后滚动时,它在indexath.row=0上崩溃,但我不明白为什么它会变成0。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // If we're displaying details, check to see if the item has a phone number, fax, and address.
    // Add one to rowCount for phone number and fax. For address, add two, so we can display a dummy
    // cell at the end. If we don't do this, the size of every cell will be the size of the address
    // cell (big) and that looks messy.

    NSInteger rowCount = 0;

    if (self.showDetails == YES)
    {
        if ([_tableRow.phone length] > 0)
        {
            rowCount++;
        }
        if ([_tableRow.fax length] > 0)
        {
            rowCount++;
        }
        if ([_tableRow.address length] > 0)
        {
            rowCount++;
            rowCount++;
        }
    }

    rowCount += [self.tableRows count];

    return rowCount;
}