问题描述:

I am trying to insert row in the section. cell is a custom cell having textfield.

Below is the code snippet,

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section

{

NSLog(@"row in section::%i %i", section, sectionRows);

if(section == 0)

return 1;

else if(section == 1)

return 1;

else

return sectionRows;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"cell rows in section%i %i", indexPath.section, [tableView numberOfRowsInSection:indexPath.section]);

static NSString *MyIdentifier = @"MyIdentifier";

MyIdentifier = @"TableView";

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 31)];

[backgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_accordian.png"]]];

CustomMyMedCellView *customCell;

UIView *viewToHide;

if(indexPath.section == 0)

{

customCell = (CustomMyMedCellView *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];

if (customCell == nil)

{

NSLog(@"in Section 1::::::");

[[NSBundle mainBundle] loadNibNamed:@"CustomMyMedCellView" owner:self options:nil];

customCell = customMyMedCellView;

[customCell setTextFieldValue:@"Teva Generic Med" editable:NO];

viewToHide= (UIView *)[customCell.contentView viewWithTag:4];

viewToHide.hidden = YES;

}

}else if(indexPath.section == 1)

{

customCell = (CustomMyMedCellView *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];

if(indexPath.row == 0)

{

if (customCell == nil)

{

[[NSBundle mainBundle] loadNibNamed:@"CustomMyMedCellView" owner:self options:nil];

customCell = customMyMedCellView;

CGRect frame = customCell.textField.frame;

frame.origin.x = 30.0;

customCell.textField.frame = frame;

[customCell setTextFieldValue:@"Accutane Capsules" editable:NO];

viewToHide = (UIView *)[customCell.contentView viewWithTag:2];

viewToHide.hidden = YES;

}

}

}else

{

customCell = (CustomMyMedCellView *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];

NSLog(@"in Section 3%@", customCell);

if (customCell == nil)

{

NSLog(@"in Section 3 if");

[[NSBundle mainBundle] loadNibNamed:@"CustomMyMedCellView" owner:self options:nil];

customCell = customMyMedCellView;

customMyMedCellView.textField.delegate = self;

CGRect frame = customCell.textField.frame;

frame.origin.x = 30.0;

customCell.textField.frame = frame;

[customCell setTextFieldValue:@"" editable:YES];

viewToHide = (UIView *)[customCell.contentView viewWithTag:2];

viewToHide.hidden = YES;

viewToHide = (UIView *)[customCell.contentView viewWithTag:4];

viewToHide.hidden = YES;

viewToHide = (UIView *)[customCell.contentView viewWithTag:5];

viewToHide.hidden = YES;

viewToHide = (UIView *)[customCell.contentView viewWithTag:6];

viewToHide.hidden = YES;

customCell.tag = indexPath.row;

[self.myMedsTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(sectionRows-1) inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

}

}

if(!self.isEditClicked)

{

viewToHide = (UIView *)[customCell.contentView viewWithTag:1];

viewToHide.hidden = YES;

}else{

viewToHide = (UIView *)[customCell.contentView viewWithTag:1];

viewToHide.hidden = NO;

}

customCell.backgroundView = backgroundView;

return customCell;

}

on clicking button i want to add row to section 2, for that below is the code i have added,

- (void)addButtonClick:(id)sender {

UIButton *clickedButton = (UIButton *) sender;

if(clickedButton.tag == 2)

{

NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];

NSInteger rowsInSection = [self.myMedsTableView numberOfRowsInSection:2];

if(rowsInSection>0){

NSLog(@"%@", [[self.myMedsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowsInSection-1 inSection:2]] viewWithTag:rowsInSection-1 ]);

CustomMyMedCellView *uiView = (CustomMyMedCellView *)[[self.myMedsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowsInSection-1 inSection:2]] viewWithTag:rowsInSection-1 ];

// NSLog(@"%@", uiView.subviews);

// UITextField *textField = (UITextField *)[uiView viewWithTag:3];

if([uiView.textField .text length] > 0)

{

sectionRows+=1;

}

}else{

sectionRows=1;

}

NSLog(@"Sectipn rows::::%i", sectionRows);

for (NSInteger i = 0; i < sectionRows; i++) {

NSLog(@"Sectipn rows:::: inside for");

[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:2]];

}

NSLog(@"%i",[indexPathsToInsert count]);

[self.myMedsTableView beginUpdates];

[self.myMedsTableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationBottom];

[self.myMedsTableView endUpdates];

//[self.myMedsTableView reloadData];

}else{

Search *tableViewController = [[Search alloc] initWithNibName:@"Search" bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:tableViewController animated:YES];

}

But i am getting below error,

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (2 inserted, 0 deleted).'

Please help me i am new to the iphone.

Thanks in Advance

网友答案:

This error occurs because there is inconsistency between the cells that are given through "cellForRowAtIndexPath" and the index paths you are trying to update. When updating your table with new cells, make sure that your "cellForRowAtIndexPath" can create a cell for these indexes and also make sure that the numberOfRowsInSection is also consistent with this information.

相关阅读:
Top