objective-c - 奇怪的错误 : insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException

标签 objective-c ios uitableview

我已经在我编写的以下示例应用程序中搜索了对 NSInternalInconsistencyException 的更合适的答案,但仍然一无所获。目标是为 tableView 的每个部分中的顶行创建展开/折叠功能。现在我尝试实现扩展部分,这适用于第 0 部分中的第 0 行。只要用户点击另一个部分中的第 0 行,就会出现此错误:

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to resolve row for index path:  2 indexes [0, 1]'

这很奇怪,因为我将表的每个 UITableViewCell 都存储在一个可变数组中。 NSMutableArray *cellForRow,其中每个索引代表表中的一个部分,每个对象都是 NSMutableArray 类型的对象。我这样做是为了避免因排队可重复使用的单元而引起的任何问题,我最初认为这些问题触发了上述异常。

异常发生在 insertRowsAtIndexPaths 语句处。我早些时候在这里读到,UITableViewController 代码必须跟踪由插入/删除引起的行数变化。我相信我用 NSMutableArray *rowsInSection 做到了,所以 UITableView 数据源方法:

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

在更改后返回部分中正确的行数。

我在代码中做错了什么以获得上述异常?


这是接口(interface)文件:

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h>

@interface MasterViewController : UITableViewController {
  NSMutableArray *rowsInSection;
  NSMutableArray *cellForRow;
}

@property (nonatomic,strong) NSMutableArray *rowsInSection;
@property (nonatomic,strong) NSMutableArray *cellForRow;

@end

这是实现文件:

#import "MasterViewController.h"

const NSInteger numSections = 4;
const NSInteger numRows = 1 + 4;
const NSInteger addRemoveRows = 4;

@implementation MasterViewController

@synthesize rowsInSection;
@synthesize cellForRow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.title = @"Table View";
        rowsInSection = [NSMutableArray arrayWithCapacity:numSections];
        cellForRow = [NSMutableArray arrayWithCapacity:numSections];
    }
    return self;
}


#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    // add number of rows for section
    for (NSInteger i = 0; i < numSections; i++) {
        [self.rowsInSection addObject:[NSNumber numberWithInteger:1]];
    }

    // container for reusable table view cells
    for (NSInteger i = 0; i < numSections; i++) {

        NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:numRows];

        for (NSInteger j = 0; j < numRows; j++) {

            // top row in section
            if (j == 0) {
                UITableViewCell *topCell = [[UITableViewCell alloc] 
                                            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                topCell.accessoryType = UITableViewCellAccessoryNone;
                topCell.contentView.backgroundColor = [UIColor whiteColor];
                topCell.textLabel.textColor = [UIColor blueColor];
                [rowsArray addObject:topCell];

                // the rest
            } else {
                UITableViewCell *simpleCell = [[UITableViewCell alloc] 
                                               initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                simpleCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                simpleCell.textLabel.textColor = [UIColor whiteColor];
                [rowsArray addObject:simpleCell];
            }
        }

        // add rows for current section into cell container
        [self.cellForRow addObject:rowsArray];
    }

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return numSections;
}

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

    NSInteger rows = [(NSNumber *)[self.rowsInSection objectAtIndex:section] integerValue];

    //NSLog(@"%@",self.rowsInSection);
    //NSLog(@"Rows: %d in section: %d",rows,section);

    return rows;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Configure the cell.

    // row count
    NSLog(@"Rows: %d in section: %d",[tableView numberOfRowsInSection:indexPath.section],indexPath.section);


    if (indexPath.row == 0) {
        UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.textLabel.text = @"TOP ROW";
        NSLog(@"Row: %d in section: %d - %@",indexPath.row,indexPath.section,cell);
        return cell;
    } else {
        UITableViewCell *cell = (UITableViewCell *)[[self.cellForRow objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"row: %d",indexPath.row];
        NSLog(@"Row: %d in section: %d - %@",indexPath.row,indexPath.section,cell);
        return cell;
    }

    // not reaching here
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"Section %d",section];
}


#pragma mark - Row editing

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // add table view cells to section if tapped on top row
    if (indexPath.row == 0 && [tableView numberOfRowsInSection:indexPath.section] == 1) {

        //NSLog(@"Selected row: %d in section: %d",indexPath.row,indexPath.section);

        NSMutableArray *indexPathArray = [NSMutableArray array];

        for (NSInteger i = 1; i <= addRemoveRows; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [indexPathArray addObject:indexPath];
        }

        // update row count for section
        NSInteger newRowCount = addRemoveRows + 1; // +1 for existing top row
        [self.rowsInSection replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInteger:newRowCount]];


        [tableView beginUpdates];
        [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
        [tableView endUpdates];

    }
}

@end

最佳答案

如果您同时插入/删除多行,则必须通过调用 beginUpdates/endUpdates 将其括起来。

TableView 数据源需要与您的插入/删除调用一致。 numberOfRowsInSection 中设置一个断点,以确保在插入/删除行后一个部分中的行数是正确的。

(我不会通读所有您的代码来为您调试它。)

祝你好运!

关于objective-c - 奇怪的错误 : insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7492804/

相关文章:

ios - 使用 AFNetworking 下载多个文件时收到内存警告

swift - TableView 单元格中的委托(delegate)错误

swift - 专门更改填充有数组元素的 TableView 中的一个单元格

ios - 从相机进行人脸检测 iOS

iphone - 如何使旋转 UIView 的方法工作多次

objective-c - OCUnit 测试嵌入式框架

ios - trackWithMediaType 有时返回 0 首轨道

swift - trailingSwipeActions 和旋转问题

ios - 半透明 modalViewController

iphone - 在 IOS 中管理内存警告。任何指针?