我已经看过stackoverflow,似乎有一些问题似乎无济于事,但没有一个能真正回答它。
我找到的最接近的问题是这里的问题,但这不是我追求的问题。
我有一个表格视图,其中充满了表格视图单元格(我们称它们为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单元)。
我想要的是单击该行时在其下方插入一个新的自定义单元格(单击一个A单元格,然后在其下方插入一个B单元格)。
有没有办法使用insertrowatindexpath做到这一点,还是有更好/不同的方式?
谢谢你们的帮助和帮助
当我单击发布问题时,它突然突然出现在我面前。
当前,在我的代码中,我总是返回headerCell(一个单元格)
我在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;
}
}
希望这可以节省人们将来花在我身上的时间
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句