2014-01-17 118 views
0

我已经看过通过stackoverflow,有一些问题似乎刷这方面,但没有实际回答它。如何将特定的tableview单元格添加到tableview中?

我发现的最近的是这个问题here但这不是我所追求的。

我有一个充满tableview单元格的tableview(让我们称它们为一个单元格)。当我点击其中一个单元格时,我想在它下面插入另一个自定义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]; 
} 

所以目前当我点击一个行下一个新行添加,但它是相同的自定义单元格的列点击(点击的小区增加下面的另一个单元格)。

我要的是下面插入一个新的自定义单元格被点击行时(点击的小区和B细胞下方插入)。

有没有办法做到这一点使用insertrowatindexpath或是否有更好的/不同的方式?对于

感谢,并帮助你们/女孩可以当我点击的问题后,突然在我面前蹦出来给

回答

0

目前在我的代码总是返回headerCell(A细胞)

我在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; 
    } 
} 

希望这将节省人们的时间它让我在未来发现

相关问题