【发布时间】: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,在调用该方法之前,您必须在数据源数组的相同索引处插入相应的项目。
标签: ios objective-c uitableview collapse