点击按钮弹出提示框添加东西到表格中包括删除cell

1.写到航

2.在vc.m里直接上代码

#import “ViewController.h”

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tbv;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource=[[NSMutableArray alloc]init];
    [self.view addSubview:self.tbv];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(leftClick)];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightClick)];
    }
    -(UITableView *)tbv{
    if (!_tbv) {
    _tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tbv.delegate=self;
    _tbv.dataSource=self;
    }
    return _tbv;
    }
    -(void)rightClick{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@“提示” message:@“请输入Section名称” preferredStyle:UIAlertControllerStyleAlert];
    //以下方法就可以实现在提示框中输入文本;

    //在AlertView中添加一个输入框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

      textField.placeholder = @"姓名";
    

    }];

    //添加一个确定按钮 并获取AlertView中的第一个输入框 将其文本赋值给BUTTON的title
    [alertController addAction:[UIAlertAction actionWithTitle:@“确定” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    UITextField *envirnmentNameTextField = alertController.textFields.firstObject;
    [self.dataSource addObject:envirnmentNameTextField.text];
    NSLog(@"%@",self.dataSource[0]);
    [self.tbv reloadData];
    //输出 检查是否正确无误
    NSLog(@“你输入的文本%@”,envirnmentNameTextField.text);

    }]];

    //添加一个取消按钮
    [alertController addAction:[UIAlertAction actionWithTitle:@“取消” style:UIAlertActionStyleDefault handler:nil]];

    //present出AlertView
    [self presentViewController:alertController animated:true completion:nil];

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@“cell”];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@“cell”];
}
cell.textLabel.text=self.dataSource[indexPath.row];
return cell;
}
-(void)leftClick{
[self.tbv setEditing:!self.tbv.isEditing animated:YES];

}

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@“commitEditingStyle–”);
    [self.dataSource removeObjectAtIndex:indexPath.row];
    [self.tbv deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }

@end

猜你喜欢

转载自blog.csdn.net/weixin_43455462/article/details/86548663