tableView中动态添加,删除行

当时在项目中完成的效果是,我在Footer中放了一个按钮,能动态生成行,并且绑定相应的文本框和删除按钮
下面实现该效果:
//setting section of number
- (NSInteger)numberOfSectionsInTableV iew:(UITableView *)tableView
{
      return 1;
}
//返回视图行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return [arrayRow count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
     
      NSString * nul = [NSString stringWithFormat:@"cell%d",indexPath.row];
      UITableViewCell *Cell = [tableView dequeueReusableCellWithI dentifier:nul];
      //Cell.accessoryType = UITableViewCellAccessory DetailDisclosureButton;
      Cell.textLabel.textAlignment = UITextAlignmentLeft;
      if(Cell == nil){
            Cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                                          reuseIdentifier:nul] autorelease];
            //add a Text
            UITextField *textEmail = [[UITextField alloc] initWithFrame:CGRectMake(30, 10, 231, 31)];
            textEmail.placeholder = @"input E-mail";
            textEmail.font = [ UIFont systemFontOfSize:14 ];
            textEmail.keyboardType = UIKeyboardTypeEmailAddre ss;
            textEmail.returnKeyType  =UIReturnKeyDone;
            textEmail.tag = indexPath.row;
            [textEmail addTarget:self action:@selector(cancelKeyBoard:) forControlEvents:UIControlEventEditingDid End];
     
            //add a button
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.contentMode = UIViewContentModeScaleTo Fill;
            UIImage *img = [UIImage imageNamed:@"BAG.png"];
            [btn setBackgroundImage:img forState:UIControlStateNormal];
            [btn setTitle:@"Button Title" forState:UIControlStateNormal];
            [btn setTag:[indexPath row]];
            [btn addTarget:self action:@selector(deletePressed:)  forControlEvents:UIControlEventTouchUpIns ide];
            btn.frame = CGRectMake(261, 10, 20, 20);
            [Cell.contentView addSubview:textEmail];
            [Cell.contentView addSubview:btn];
            Cell.selectionStyle = UITableViewCellSelection StyleNone;
            [textEmail release];
      }
      return Cell;
}

UIButton *addBtn;
//add row in tableview and datasourse
- (IBAction) addPressed:(id)sender
{
      addBtn = (UIButton*)sender;
      [arrayRow addObject:@"Add"];
      if ([arrayRow count] ==4) {
            [sender setHidden:YES];
      }
      NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[arrayRow count]-1 inSection:0];
      [self.tableViewEmail insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationF ade];
}

//delete row in tableview and datasourse
-(void)deletePressed:(id)sender
{
      if ([arrayRow count] ==1) {
            return;
      }
      if ([arrayRow count] <= 4) {
            [addBtn setHidden:NO];
      }
     
      NSInteger idx = [sender tag];
     
      if (idx >=[arrayRow count]) {
            [sender setTag:idx-1];
      }
      [arrayRow removeObjectAtIndex:[sender tag]];
      NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
      [self.tableViewEmail beginUpdates];
      [self.tableViewEmail deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationF ade];
      [self.tableViewEmail endUpdates];     
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值