【发布时间】:2014-01-17 09:41:43
【问题描述】:
我已经查看了 stackoverflow,有一些问题似乎没有解决这个问题,但没有一个真正回答它。
我找到的最接近的是这个问题here,但这不是我想要的。
我有一个表格视图,其中充满了表格视图单元格(我们称它们为 A 单元格)。当我单击其中一个单元格时,我想在其下方插入另一个自定义表格视图单元格(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 或者有更好/不同的方法吗?
感谢你们的帮助和帮助
【问题讨论】:
标签: ios objective-c uitableview insert