无效更新:第 0 节中的无效行数。更新后现有节中包含的行数 (3)
Posted
技术标签:
【中文标题】无效更新:第 0 节中的无效行数。更新后现有节中包含的行数 (3)【英文标题】:Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) 【发布时间】:2016-10-31 17:39:01 【问题描述】:任何人都可以帮助我,我是 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];
【问题讨论】:
您必须始终保持数据源数组和 UI 同步。insertRowsAtIndexPaths
只更新 UI,在调用该方法之前,您必须在数据源数组的相同索引处插入相应的项目。
【参考方案1】:
您遇到此问题是因为您需要在调用时更改数据源(为新行添加对象)
[[self tbldata] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
详情见docs
【讨论】:
【参考方案2】:我已经像 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;
【讨论】:
我会尝试并检查它的兄弟 还要添加这两行 [tableview beginUpdates]; & [tableview endUpdates];插入行之前和之后。 原因:'无效更新:第 0 节中的行数无效。更新 (3) 后现有节中包含的行数必须等于之前该节中包含的行数更新(3), 上面两行你加了吗? 在插入行之前为您的部分计算行数。以上是关于无效更新:第 0 节中的无效行数。更新后现有节中包含的行数 (3)的主要内容,如果未能解决你的问题,请参考以下文章
无效更新:第 0 节中的无效行数以 NSException 类型的未捕获异常终止