2016-06-29 3 views
2

Может ли кто-нибудь мне помочь, я новичок в разработке iOS, что я делаю неправильно в своем коде , Когда я нажимаю на строку, она должна вставить другую строку ниже.Недопустимое обновление: недопустимое количество строк в разделе 0. Количество строк, содержащихся в существующем разделе после обновления (3)

Вот мой код

Ошибка: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. 
} 

Прагма 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]; 
} 
+0

Вы должны всегда держать массив исходных данных и пользовательский интерфейс в синхронизации , 'insertRowsAtIndexPaths' обновляет только пользовательский интерфейс, прежде чем вы вызовете метод, вы должны вставить соответствующие элементы в те же индексы в массиве источников данных. – vadian

ответ

1

я обновляемые как дис

-(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; 
} 
+0

i wil попробуйте и попробуйте его. –

+1

Также добавьте эту две строки [tableview beginUpdates]; & [tableview endUpdates]; до и после вставки строк. –

+0

с той же ошибкой bro like dis –

1

ваш имеет эту проблему, потому что вам нужно изменить источник данных (добавить объект для новой строки) при вызове

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

Подробнее в docs

Смежные вопросы

 Смежные вопросы