Ios 如何将特定的tableview单元格添加到tableview中?
我已经浏览了stackoverflow,有一些问题似乎触及了这个问题的侧面,但没有一个真正回答这个问题 我找到的最接近的问题是这个问题,但这不是我想要的 我有一个tableview,其中充满了tableview单元格(让我们称它们为单元格)。单击其中一个单元格时,我希望在其下方插入另一个自定义tableview单元格(B单元格) 我的其他答案一直涉及隐藏和显示单元格,但我希望能够多次单击以继续添加更多自定义单元格 这就是我目前正在处理的问题:Ios 如何将特定的tableview单元格添加到tableview中?,ios,objective-c,uitableview,insert,Ios,Objective C,Uitableview,Insert,我已经浏览了stackoverflow,有一些问题似乎触及了这个问题的侧面,但没有一个真正回答这个问题 我找到的最接近的问题是这个问题,但这不是我想要的 我有一个tableview,其中充满了tableview单元格(让我们称它们为单元格)。单击其中一个单元格时,我希望在其下方插入另一个自定义tableview单元格(B单元格) 我的其他答案一直涉及隐藏和显示单元格,但我希望能够多次单击以继续添加更多自定义单元格 这就是我目前正在处理的问题: -(id) initWithGuestNumber:
-(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];
}
-(id)initWithGuestNumber:(NSInteger*)numberOfPeople{
self=[super initWithNibName:Nil bundle:Nil];
如果(自我){
numberOfGuests=*numberOfPeople;
guestArray=[NSMutableArray new];
例如(NSInteger i=1;i我一点击post-question,它就突然出现在我面前
目前在我的代码中,我总是返回headerCell(一个单元格)
我在CellForRowatinex中稍微转换了代码,它开始工作:
-(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;
}
}
希望这能节省人们在未来我发现的时间