2011-10-14 4 views
0

섹션에 행을 삽입하려고합니다. 셀은 텍스트 필드가있는 사용자 정의 셀입니다.동적으로 tableview의 섹션에 행 삽입 iphone

다음

코드 조각이다,

내가 그 아래를 들면, 2 절에 행을 추가 할 버튼을 클릭에
-(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; 
} 

내가 추가 한 코드,

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

하지만 난 무엇입니까 아래 오류 :

'NSInternalInconsistencyException'이라는 캐치되지 않은 예외로 인해 앱이 종료됩니다. 이유 : 잘못된 업데이트 : 섹션 2의 행 수가 잘못되었습니다. 기존 섹션에 포함 된 행 수 update (2)가 업데이트 (1) 이전에 해당 섹션에 포함 된 행 수와 같거나 그 섹션 (삽입 된 2 개, 삭제 된 0 개)에서 삽입 또는 삭제 된 행 수를 더하거나 뺀 값이어야합니다.

저는 아이폰에 익숙하지 않습니다. 미리 감사드립니다.

답변

1

이 오류는 "cellForRowAtIndexPath"를 통해 제공된 셀과 업데이트하려는 인덱스 경로가 일치하지 않기 때문에 발생합니다. 새 셀로 표를 업데이트 할 때는 "cellForRowAtIndexPath"가 이러한 색인의 셀을 만들 수 있는지 확인하고 numberOfRowsInSection도이 정보와 일치하는지 확인하십시오.

+1

또 다른 말은 endUpdates를 호출 할 때까지 모델과보기가 동일한 정보를 포함해야한다는 것입니다. 뷰에 행을 추가하는 경우 해당 객체를 모델에 추가해야합니다. – sosborn