无效更新:第0节中的行数无效。更新后现有部分中包含的行数(3)

时间:2016-06-29 06:55:28

标签: ios objective-c uitableview collapse

任何人都可以帮助我,我是iOS开发的新手,我在我的代码中做错了。当我点击该行时,它应在其下方插入另一行。

这是我的代码

错误: reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3)

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

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
    [self setupViewController];
}
return self;
}



- (void)setupViewController
{
NSArray* colors = [[NSArray alloc]initWithObjects:@"PENDENTS",@"EARRINGS",@"PENDENT SETS", nil];

self.data = [[NSMutableArray alloc] init];

for (int i = 0 ; i < [colors count] ; i++)
{
    NSMutableArray* section = [[NSMutableArray alloc] init];

    for (int j = 0 ; j < 1 ; j++)
    {
        [section addObject:[NSString stringWithFormat:@"EAR RINGS  ", j]];
        [section addObject:[NSString stringWithFormat:@"PENDENT SETS ",j]];
        [section addObject:[NSString stringWithFormat:@"PENDENTS ",j]];
        [section addObject:[NSString stringWithFormat:@"25 pieces ",j]];
        [section addObject:[NSString stringWithFormat:@"329.672 ",j]];
        [section addObject:[NSString stringWithFormat:@"may 1 ",j]];
        [section addObject:[NSString stringWithFormat:@"may 10 ",j]];
        [section addObject:[NSString stringWithFormat:@"8,40,000 ",j]];
    }

    [self.data addObject:section];
}

self.headers = [[NSMutableArray alloc] init];

for (int i = 0 ; i < [colors count] ; i++)
{

    UIView* header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 40)];
    UILabel *lblpend=[[UILabel alloc]initWithFrame:CGRectMake(12, 0, 170, 40)];
    UILabel *lbldeliver=[[UILabel alloc]initWithFrame:CGRectMake(332, 0,113,40)];
    UILabel *lbltotal =[[UILabel alloc]initWithFrame:CGRectMake(562, 0, 200, 40)];

    [lblpend setText:@"RM-15/16-GRT-0102-CHE"];
    [lbldeliver setText:@"Delivered:27 Mar"];
    [lbltotal setText:@"941.92gms  Rs24,00,000"];

    [lbltotal setFont:[UIFont systemFontOfSize:14]];
    [lbldeliver setFont:[UIFont systemFontOfSize:14]];
    [lblpend setFont:[UIFont systemFontOfSize:14]];

    [header addSubview:lblpend];
    [header addSubview:lbldeliver];
    [header addSubview:lbltotal];
    [header setBackgroundColor:[UIColor greenColor]];
    [self.headers addObject:header];
}
}


- (void)viewDidLoad {

[super viewDidLoad];

self.expandablecells=[[NSMutableArray alloc]initWithObjects:@"cozycoracle",@"pendents",@"pendent sets", nil];

isTapped=YES;

[self.tbldata reloadData];
[self.tbldata openSection:0 animated:NO];
[super viewDidLoad];
// Do any additional setup after loading the view.
}

pragma tableview委托

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isTapped==YES) {

    isTapped=NO;

    return [self.expandablecells count];
}
return [self.data count];


}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (isTapped==YES) {

    isTapped=NO;

    return [self.expandablecells count];

}

return [self.data count];


}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"cell";

MyOrderCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell)
{
    cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}


if (isTapped==NO) {

    //isTapped=YES;

    NSString* text = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell.lblpendnts.text = text;
    cell.lblpieces.text=text;
    cell.lblnum.text=text;
    cell.lbldate.text=text;
    cell.lblenddate.text=text;
    cell.lbltotl.text=text;



}else{

    cell.lblpendnts.text = [self.expandablecells objectAtIndex:indexPath.row];
    cell.lblpieces.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lblnum.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lbldate.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lblenddate.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lbltotl.text=[self.expandablecells objectAtIndex:indexPath.row];

}


return cell;
}

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

MyOrderCell *cell=[[MyOrderCell alloc]init];

if (isTapped==NO) {

    //isTapped=NO;

    if (indexPath.section == 0 & indexPath.row==0) {
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
        indexPath = newPath;

        [[self tbldata] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];


    }

}

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [self.headers objectAtIndex:section];
}

2 个答案:

答案 0 :(得分:1)

你有这个问题,因为你需要在调用

时更改数据源(为新行添加一个对象)
[[self tbldata] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

请参阅docs

中的详细信息

答案 1 :(得分:0)

我更新了像dis

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

if (isTapped==NO) {

    //isTapped=NO;

    if (indexPath.section == 0 & indexPath.row==0) {


        section=indexPath.section;
        numOfRows=indexPath.row;

        NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:[self.data count]];
        [self.tbldata beginUpdates];


        [self.tbldata insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom];
        [self.tbldata endUpdates];



    }
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(NSArray*) indexPathsForSection:(NSInteger)section withNumberOfRows:(NSInteger)numberOfRows
{
NSMutableArray* indexPaths = [NSMutableArray new];
for (int i = 0; i < numberOfRows; i++) {
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:section];
    [indexPaths addObject:indexPath];
}
return indexPaths;
}