2016-06-21 55 views
0

有两个textFields的单元格...然后点击ADD按钮,我想在tableview中动态添加该单元格。并且还删除行。 而在两个文本框不应该得到滚动过程中,并增加新的细胞变化的文字...如何在uitableviewcontroller中动态添加单元格?

而在我想解释的阵列中的所有文本字段值结束..

下面的代码中,我曾尝试

- (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]; 
} 
+0

可能是这个帮助你http://stackoverflow.com/questions/31870206/how-to-insert-new-cell-into-uitableview-in-swift/31870301#31870301 –

+0

我已经实现了这个...但随着单元格数量的增加,新的单元格textfields默认取消了单元格的值... –

+0

@AkshayAmbekar数组是否已初始化? itemsArray ='[[[NSMutableArray alloc] init];' – pnizzle

回答

2

您的单元格将显示重复的文本字段值,因为您正在重新使用它们,而不是调整单元格细节,现在希望单元格代表不同的对象。

如果你不想要这种行为(你显然不需要),那么你需要在你的dataSource上设置单元格的细节。然后在cellForRowAtIndexPath()中,用相应的对象细节填充当前单元格。或者,我不建议,您可以为每个添加的项目在cellForRowAtIndexPath()中为init分配一个新的单元格。当屏幕上不可见的单元格时,这会不必要地占用更多内存。

这里是一个例子:这是写得很快,只演示动态填充数据源中的单元格,其他所有内容都不是建议。

#import "PeopleTableViewController.h" 

//--- Person Class 
@interface Person : NSObject<UITextFieldDelegate> 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *surname; 
@end 
@implementation Person 
@end 
//--- 

#define CELL_HEIGHT 80.0f 
#define TEXTFIELD_HEIGHT 30.0f 
#define TEXTFIELD_WIDTH 120.0f 
#define TEXT_FIELD_VERTICAL_MARGIN (CELL_HEIGHT - (TEXTFIELD_HEIGHT*2))/3 
#define TEXT_FIELD_LEFT_MARGIN 20.0f 
//--- Person Cell 
@class PersonCell; 
@protocol PersonCellTextFieldProtocol <NSObject> 
-(void)personCell: (PersonCell*)cell nameTextfieldDidChange: (NSString*)newText; 
-(void)personCell: (PersonCell*)cell surnameTextfieldDidChange: (NSString*)newText; 
@end 
@interface PersonCell : UITableViewCell<UITextFieldDelegate> 
@property (nonatomic, strong) UITextField *nameTextField; 
@property (nonatomic, strong) UITextField *surnameTextField; 
@property (nonatomic, strong) UILabel *positionLabel; 
@property (nonatomic, assign) id<PersonCellTextFieldProtocol> delegate; 
@end 
@implementation PersonCell 
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if(self) 
     [self setupSubviews]; 
    return self; 
} 
-(void)setupSubviews 
{ 
    NSUInteger nameTextFieldYPosition = TEXT_FIELD_VERTICAL_MARGIN; 

    self.nameTextField = [[UITextField alloc] initWithFrame: CGRectMake(TEXT_FIELD_LEFT_MARGIN, nameTextFieldYPosition, TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT)]; 
    self.nameTextField.borderStyle = UITextBorderStyleRoundedRect; 
    self.nameTextField.placeholder = @"Name"; 
    self.nameTextField.delegate = self; 
    [self.nameTextField addTarget:self action:@selector(nameTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 
    [self.contentView addSubview: self.nameTextField]; 

    self.surnameTextField = [[UITextField alloc] initWithFrame: CGRectMake(TEXT_FIELD_LEFT_MARGIN, CGRectGetMaxY(self.nameTextField.frame) + TEXT_FIELD_VERTICAL_MARGIN, TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT)]; 
    self.surnameTextField.borderStyle = UITextBorderStyleRoundedRect; 
    self.surnameTextField.placeholder = @"Surname"; 
    self.surnameTextField.delegate = self; 
    [self.surnameTextField addTarget:self action:@selector(surnameTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 
    [self.contentView addSubview: self.surnameTextField]; 

    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); 
} 

-(void)layoutSubviews 
{ 
    self.positionLabel.center = CGPointMake(self.positionLabel.center.x, self.contentView.center.y); 
} 

-(void)nameTextFieldDidChange:(UITextField*)textField 
{ 
    if([self.delegate respondsToSelector: @selector(personCell:nameTextfieldDidChange:)]) 
     [self.delegate personCell:self nameTextfieldDidChange:textField.text]; 
} 

-(void)surnameTextFieldDidChange:(UITextField*)textField 
{ 
    if([self.delegate respondsToSelector: @selector(personCell:surnameTextfieldDidChange:)]) 
     [self.delegate personCell:self surnameTextfieldDidChange:textField.text]; 
} 

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return YES; 
} 
@end 
//--- 

@interface PeopleTableViewController()<PersonCellTextFieldProtocol> 
@end 

@implementation PeopleTableViewController 
{ 
    NSMutableArray *_peopleArray; 
} 

-(void)viewDidLoad 
{ 
    _peopleArray = [[NSMutableArray alloc] init]; 
    [self.tableView reloadData]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PersonCell *cell = nil; 
    cell = [tableView dequeueReusableCellWithIdentifier: @"CellWithNameAndSurname"]; 
    if(!cell) 
    { 
     cell = [[PersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithNameAndSurname"]; 
     cell.contentView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent: 0.08f]; 
     cell.delegate = self; 
    } 

    //this should be outside the above if statement! 
    Person *respectivePerson = _peopleArray[indexPath.row]; 
    cell.nameTextField.text = respectivePerson.name; 
    cell.surnameTextField.text = respectivePerson.surname; 
    cell.positionLabel.text = [NSString stringWithFormat:@"%i", (int)indexPath.row]; 

    return cell; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CELL_HEIGHT; 
} 

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _peopleArray.count; 
} 

-(void)personCell:(PersonCell *)cell nameTextfieldDidChange:(NSString *)newText 
{ 
    Person *respectivePerson = _peopleArray[[self.tableView indexPathForCell: cell].row]; 
    respectivePerson.name = newText; 
} 

-(void)personCell:(PersonCell *)cell surnameTextfieldDidChange:(NSString *)newText 
{ 
    Person *respectivePerson = _peopleArray[[self.tableView indexPathForCell: cell].row]; 
    respectivePerson.surname = newText; 
} 

- (IBAction)addItemBtnClick:(id)sender 
{ 
    Person *newPerson = [Person new]; 

    [_peopleArray addObject: newPerson]; 

    [self.tableView reloadData]; 
} 

@end 

当我点击添加,我得到没有任何重复如下表,当我滚动:

enter image description here

+0

我试图从cellForRowAtIndexPath()中的数据源中填充单元格...但随后单元格获得随机值并仍然出现问题。你能建议我有其他的选择吗?请告诉我如何为每个项目分配init新单元格cellForRowAtIndexPath? –

+0

@AkshayAmbekar看看我刚刚添加的示例。继续询问你是否不确定某件事。 – pnizzle

+0

无法使用UItextField –

1

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

在swift中看起来像这样,但我相信你可以在目标c中找到相同的代码。

yourTableViewOutlet.reloadData() 

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

0

你应该把这个当你插入或删除的UITableViewCell:

**[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];** 
相关问题