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

gmxoilav  于 2022-12-01  发布在  iOS
关注(0)|答案(2)|浏览(156)

有人能帮我吗,我是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表视图委托

- (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];
}
watbbzwu

watbbzwu1#

您遇到此问题是因为您需要在调用时更改数据源(为新行添加对象)

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

详见文档

qnzebej0

qnzebej02#

我有更新喜欢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;
}

相关问题