簡體   English   中英

將行插入表格視圖中的部分

[英]Inserting row to the section in tableview dyanmically iphone

我試圖在該部分中插入行。 cell是具有文本字段的自定義單元格。

下面是代碼片段,

-(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新手。 提前致謝

發生此錯誤的原因是,通過“ cellForRowAtIndexPath”指定的單元格與您嘗試更新的索引路徑之間存在不一致。 使用新的單元格更新表時,請確保“ cellForRowAtIndexPath”可以為這些索引創建一個單元格,並確保numberOfRowsInSection也與此信息一致。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM