在tableview动态iphone中插入行

时间:2011-10-14 08:31:10

标签: iphone dynamic row tableview

我正在尝试在该部分中插入行。 cell是具有textfield的自定义单元格。

以下是代码段

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

点击按钮我想在第2部分添加行,因为下面是我添加的代码

-  (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节中的行数无效。更新后的现有部分中包含的行数(2)必须等于行数在更新(1)之前包含在该部分中,加上或减去从该部分插入或删除的行数(插入2个,删除0个)。'

请帮助我,我是iphone的新手。 在此先感谢

1 个答案:

答案 0 :(得分:1)

发生此错误是因为通过“cellForRowAtIndexPath”提供的单元格与您尝试更新的索引路径之间存在不一致。使用新单元格更新表格时,请确保“cellForRowAtIndexPath”可以为这些索引创建单元格,并确保numberOfRowsInSection也与此信息一致。

相关问题