如何将特定的tableview单元格添加到tableview中?

时间:2014-01-17 01:34:43

标签: ios objective-c uitableview insert

我已经查看了stackoverflow,并且有一些问题似乎刷了这个方面,但没有一个实际回答它。

我找到的最接近的是这个问题here,但这不是我所追求的。

我有一个用tableview单元格填充的tableview(让我们称之为A单元格)。当我单击其中一个单元格时,我想在其下面插入另一个自定义tableview单元格(B单元格)。

我总是涉及隐藏和显示单元格的其他答案,但我希望能够多次单击以继续添加更多这些自定义单元格。

这就是我目前正在使用的内容:

-(id) initWithGuestNumber: (NSInteger *) numberOfPeople {
    self = [super initWithNibName:Nil bundle:Nil];
    if (self) {

        numberOfGuests = * numberOfPeople;
        guestArray = [NSMutableArray new];

        for (NSInteger i = 1; i <= numberOfGuests; i++) {

            BGuest * guest = [BGuest alloc];
            NSMutableArray * array = [NSMutableArray new];

            // Set up guest object
            guest.name = [NSString stringWithFormat:@"Person %li", (long)i];
            guest.mealArray = array;
            guest.priceArray = array;
            guest.cost = 0;

            [guestArray addObject:guest];
        }
    }
     return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set up custom tableview cells
    [self.tableView registerNib:[UINib nibWithNibName:@"BHeaderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HeaderCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BContentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ContentCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView_
{
    // Want a section for each guest
    return guestArray.count;
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BGuest * guest = guestArray[section];
    // Want a row in the section for each meal added to each guest - by clicking the row
    return guest.mealArray.count + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BHeaderCell * headerCell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];
    BContentCell * contentCell = [tableView dequeueReusableCellWithIdentifier:@"ContentCell"];

    // Setting the delegate programatically to set the textfield


    BGuest * guest = guestArray[indexPath.section];

    if (indexPath.row == 0) {

        headerCell.guestNameTextField.delegate = headerCell;
        headerCell.guestNameTextField.placeholder = guest.name;
        headerCell.guestMealPriceLabel.text = [NSString stringWithFormat:@"£%.2f", guest.cost];
}
    else {

        contentCell.mealNameTextField.delegate = contentCell;
        contentCell.mealCostTextField.delegate = contentCell;

        contentCell.mealNameTextField.text = @"Meal Name";
        contentCell.mealCostTextField.text = @"Meal Cost";
    }

    return headerCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BGuest * guest = guestArray[indexPath.section];

    NSString * first = @"first";
    [guest.mealArray addObject:first];

    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

所以目前当我单击一行时,下面会添加一个新行,但它与单击的行是相同的自定义单元格(单击A单元格会在下面添加另一个A单元格)。

我想要的是在单击行时在下面插入一个新的自定义单元格(单击一个单元格,下面插入一个B单元格)。

使用insertrowatindexpath有没有办法做到这一点,还是有更好/不同的方式?

感谢并帮助你们/女孩们给予

1 个答案:

答案 0 :(得分:0)

当我点击帖子问题时,它突然突然出现在我面前。

目前在我的代码中,我总是返回headerCell(A cell)

我在cellForRowAtIndex中略微转换了代码并开始工作:

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

    // Setting the delegate programatically to set the textfield


    BGuest * guest = guestArray[indexPath.section];

    if (indexPath.row == 0) {

        BHeaderCell * headerCell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];

        headerCell.guestNameTextField.delegate = headerCell;

        headerCell.guestNameTextField.placeholder = guest.name;

        headerCell.guestMealPriceLabel.text = [NSString stringWithFormat:@"£%.2f", guest.cost];

        return headerCell;
    }
    else {

        BContentCell * contentCell = [tableView dequeueReusableCellWithIdentifier:@"ContentCell"];

        //contentCell.mealNameTextField.delegate = contentCell;
        //contentCell.mealCostTextField.delegate = contentCell;

        contentCell.mealNameTextField.text = @"Yeah";
        contentCell.mealCostTextField.text = @"£1.50";

        return contentCell;
    }
}

希望这可以节省人们将来发现的时间

相关问题