Ios 如何在uitableviewcontroller中动态添加单元格?

Ios 如何在uitableviewcontroller中动态添加单元格?,ios,objective-c,uitableview,tableviewcell,Ios,Objective C,Uitableview,Tableviewcell,有两个文本字段的单元格。。。单击ADD按钮,我想在tableview中动态添加该单元格。并删除行。 在滚动和添加新单元格的过程中,两个文本字段上的文本不应得到更改 最后,我需要字典数组中的所有文本字段值 遵循我尝试过的代码 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numb

有两个文本字段的单元格。。。单击ADD按钮,我想在tableview中动态添加该单元格。并删除行。 在滚动和添加新单元格的过程中,两个文本字段上的文本不应得到更改

最后,我需要字典数组中的所有文本字段值

遵循我尝试过的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [itemsArray count];
}

-(void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addItemCellIdentifier"];
    if (cell == nil) {
        cell = [[AddItemTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"addItemCellIdentifier"];
    }
    return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

       [itemsArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

- (IBAction)addItemBtnClick:(id)sender {


    if ([itemsArray count]) {
        [itemsArray removeLastObject];
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        AddItemTableViewCell *Cell = (AddItemTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
        UITextField * name = (UITextField *)[Cell.contentView viewWithTag:11];
        UITextField * Qty = (UITextField *)[Cell.contentView viewWithTag:12];
        NSMutableDictionary *item = [NSMutableDictionary new];
        [item setObject:name.text forKey:@"item"];
        [item setObject:Qty.text forKey:@"quantity"];
        [itemsArray addObject:item];
    }
    [itemsArray addObject:@{@"item":@"",@"quantity":@""}];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

我认为你的问题是,在你添加了一个单元格后,它不会显示在表中。在itemArray字典中进行更改后,重新加载tableview的数据

在swift中,它看起来像这样,但我相信您可以在objective c中找到相同的代码

yourTableViewOutlet.reloadData()

无论何时更改用于填充/定义tableView数据的词典,只需重新加载数据即可

我认为您的问题是,添加单元格后,它不会显示在表中。在itemArray字典中进行更改后,重新加载tableview的数据

在swift中,它看起来像这样,但我相信您可以在objective c中找到相同的代码

yourTableViewOutlet.reloadData()

无论何时更改用于填充/定义tableView数据的词典,只需重新加载数据即可

您的单元格将显示重复的textfield值,因为您正在重复使用这些值,而不是调整单元格的详细信息,此时单元格将表示不同的对象

如果不希望出现这种行为(显然不希望),则需要在数据源上设置单元格详细信息。然后在cellforrowatinedexpath()中,使用相应的对象详细信息填充当前单元格。或者,我不建议您为添加的每个项目在CellForRowatineXpath()中分配一个新的单元格。当您的单元格在屏幕上不可见时,这将不必要地占用更多内存

这里有一个例子:写这篇文章只是为了演示从数据源动态填充单元格,其他一切都不是建议

#导入“PeopleTableViewController.h”
//---人类
@接口人:NSObject
@属性(非原子,强)NSString*名称;
@属性(非原子,强)NSString*姓氏;
@结束
@执行人
@结束
//---
#定义单元高度80.0f
#定义文本字段高度30.0f
#定义文本字段\u宽度120.0f
#定义文本\字段\垂直\边距(单元格\高度-(文本字段\高度*2))/3
#定义文本\u字段\u左边距\u 20.0f
//---人细胞
@班级人员;
@协议PersonCellTextFieldProtocol
-(void)personCell:(personCell*)单元格名称TextFieldDidChange:(NSString*)新文本;
-(void)personCell:(personCell*)单元格姓氏TextFieldDidChange:(NSString*)新文本;
@结束
@接口PersonCell:UITableViewCell
@属性(非原子,强)UITextField*nameTextField;
@属性(非原子,强)UITextField*姓氏textfield;
@属性(非原子,强)UILabel*positionLabel;
@属性(非原子,赋值)id委托;
@结束
@执行人员
-(instancetype)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符
{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我)
[自设置子视图];
回归自我;
}
-(无效)设置子视图
{
NSU整数名称TextFieldyPosition=文本\字段\垂直\边距;
self.nameTextField=[[UITextField alloc]initWithFrame:CGRectMake(文本字段左边框,名称文本字段位置,文本字段宽度,文本字段高度)];
self.nameTextField.borderStyle=UITextBorderStyleRoundedRect;
self.nameTextField.placeholder=@“Name”;
self.nameTextField.delegate=self;
[self.nameTextField addTarget:self action:@selector(nameTextFieldDidChange:)for controlEvents:UIControlEventEditingChanged];
[self.contentView addSubview:self.nameTextField];
self.nameTextField.frame=[[UITextField alloc]initWithFrame:CGRectMake(TEXT\u FIELD\u LEFT\u MARGIN,CGRectGetMaxY(self.nameTextField.frame)+TEXT\u FIELD\u VERTICAL\u MARGIN,TEXTFIELD\u WIDTH,TEXTFIELD\u HEIGHT)];
self.namestTextField.borderStyle=UITextBorderStyleRoundedRect;
self.namestextfield.placeholder=@“姓氏”;
self.namestextfield.delegate=self;
[self.namestTextField addTarget:self action:@selector(namestTextFieldDidChange:)for ControlEvents:UIControlEventEditingChanged];
[self.contentView addSubview:self.namestextfield];
self.positionLabel=[[UILabel alloc]initWithFrame:CGRectMake(5,0,10,20)];
self.positionLabel.font=[UIFont systemFontOfSize:10];
[self.contentView addSubview:self.positionLabel];
self.positionLabel.center=CGPointMake(self.positionLabel.center.x,self.contentView.center.y);
}
-(无效)布局子视图
{
self.positionLabel.center=CGPointMake(self.positionLabel.center.x,self.contentView.center.y);
}
-(无效)nameTextFieldDidChange:(UITextField*)textField
{
if([self.delegate respondsToSelector:@selector(personCell:nameTextfieldDidChange:)]))
[self.delegate personCell:self-nameTextfieldDidChange:textField.text];
}
-(无效)姓氏TextFieldDidChange:(UITextField*)textField
{
if([self.delegate respondsToSelector:@selector(personCell:namestTextFieldDidChange:)]))
[self.delegate personCell:self姓氏textField didchange:textField.text];
}
-(BOOL)textField应返回:(UITextField*)textField
{
[textField resignFirstResponder];
返回YES;
}
@结束
//---
@接口PeopleTableViewController()
@结束
@PeopleTableViewController的实现
{
NSMutableArray*_peopleArray;
}
-(无效)viewDidLoad
{
_peopleArray=[[NSMutableArray alloc]init];
[self.tableView重载数据];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
PersonCell*单元格=零;
cell=[tableView dequeueReusableCellWithIdentifier:@“CellWithNameandNames”];
如果(!单元格)
{
cell=[[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“CellWithNameandNames”];
cell.contentView.backgroundColor=[[UIC]
**[self.tableView beginUpdates];**
if ([itemsArray count]) {
[itemsArray removeLastObject];
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
AddItemTableViewCell *Cell = (AddItemTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
UITextField * name = (UITextField *)[Cell.contentView viewWithTag:11];
UITextField * Qty = (UITextField *)[Cell.contentView viewWithTag:12];
NSMutableDictionary *item = [NSMutableDictionary new];
[item setObject:name.text forKey:@"item"];
[item setObject:Qty.text forKey:@"quantity"];
[itemsArray addObject:item];

}
[itemsArray addObject:@{@"item":@"",@"quantity":@""}];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

**[self.tableView endUpdates];**