Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 奇怪的错误:UITableView中的insertRowsAtIndexPaths与NSInternalInconsistencyException一起崩溃_Objective C_Ios_Uitableview - Fatal编程技术网

Objective c 奇怪的错误:UITableView中的insertRowsAtIndexPaths与NSInternalInconsistencyException一起崩溃

Objective c 奇怪的错误:UITableView中的insertRowsAtIndexPaths与NSInternalInconsistencyException一起崩溃,objective-c,ios,uitableview,Objective C,Ios,Uitableview,对于我在下面编写的示例应用程序中收到的nsinternalinconsistenceexception,我已经搜索了一个更合适的答案,但仍然没有找到答案。目标是为tableView的每个部分的顶行创建展开/折叠功能。现在我尝试实现扩展部分,这适用于第0节中的第0行。一旦用户点击另一节中的第0行,就会出现此错误: 返回更改后节中正确的行数 为了获得上述异常,我在代码中做错了什么 这是接口文件: #import <UIKit/UIKit.h> #import <Qua

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

返回更改后节中正确的行数

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


这是接口文件:

#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

#导入
#进口
@界面主视图控制器:UITableViewController{
NSMUTABLEARRY*rowsInSection;
NSMutableArray*cellForRow;
}
@属性(非原子,强)NSMUTABLEARRY*rowsInSection;
@属性(非原子,强)NSMutableArray*cellForRow;
@结束

这是实施文件:

#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

#导入“MasterViewController.h”
const NSInteger numSections=4;
const NSInteger numRows=1+4;
常数NSInteger ADDROVEROWS=4;
@主视图控制器的实现
@综合切片;
@合成细胞因子;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
self.title=@“表视图”;
rowsInSection=[NSMUTABLEARRY arrayWithCapacity:numSections];
cellForRow=[NSMutableArray arrayWithCapacity:numSections];
}
回归自我;
}
#pragma标记-视图生命周期
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
self.tableView.backgroundColor=[UIColor clearColor];
self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
self.tableView.dataSource=self;
self.tableView.delegate=self;
//为节添加行数
对于(NSInteger i=0;i对于(NSInteger i=1;i您不仅要插入单元格,还要删除旧行,但是表视图不知道它,因为您没有告诉它。因此表视图“知道”它有一行,然后您告诉它您
#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
-(void)tableView:(UITableView)tableView deleteRowsAtIndexPaths:(NSArray)indexPath withRowAnimation:(UITableViewRowAnimation)animation;