便签(表格)的编辑 增加与删除

#import “DataManager.h”
#import “ViewController.h”

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic , strong)NSMutableArray *SeleArr;// 数组
@property(nonatomic,strong)Model *m_model;
@property(nonatomic,strong)NSString *g_str;
@end

@implementation ViewController
{
NSMutableArray *items;
}

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.m_model = [[Model alloc]init];
    self.strUrl = [[Model alloc]init];
    self.g_str = [NSString string];
    self.SeleArr = [[NSMutableArray alloc]init];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBegin)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@“edit” style:UIBarButtonItemStyleDone target:self action:@selector(edit)];
    //

}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

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

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“cell”];
    if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“cell”];
    }
    Model *modelIII = self.SeleArr[indexPath.row];
    cell.textLabel.text = modelIII.title_Arr;

    return cell;
    }
    //可以编辑时 显示动画效果 ,不能编辑时 收回动画效果
    -(void)edit
    {
    if (_tableView.editing == YES) {
    [self.tableView setEditing:NO animated:YES];
    }else{
    [self.tableView setEditing:YES animated:NO];
    }
    }

-(void)addBegin{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@“提示” message:@“请输入要插入的信息” preferredStyle:UIAlertControllerStyleAlert];
//增加确定按钮;
[alertController addAction:[UIAlertAction actionWithTitle:@“确定” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//获取第1个输入框;
UITextField *userNameTextField = alertController.textFields.firstObject;
self.g_str = userNameTextField.text;
//把主动加入的字符串加入model
self.strUrl.title_Arr = self.g_str;

    [[DataManager shareManage]open];
    [[DataManager shareManage]create];
    [[DataManager shareManage]insert:self.strUrl];
    self.SeleArr = self.SeleArr = [[DataManager shareManage]select];
    NSLog(@"self.array---------------------------%@",self.SeleArr);
    [self.tableView reloadData];
    
}]];
//增加取消按钮;
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
//定义第一个输入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.placeholder = @"请输入信息";
}];

[self presentViewController:alertController animated:true completion:nil];

}
//查询 插入
-(void)viewWillAppear:(BOOL)animated{
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.tableView.scrollEnabled = YES;

self.tableView.delegate =self ;
self.tableView.dataSource = self;

[self.view addSubview:self.tableView];
self.SeleArr = [[DataManager shareManage] select];
[[DataManager shareManage]insert:self.strUrl];
//
[self.tableView reloadData];
}

//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    //打开数据库
    [[DataManager shareManage]open];

    [[DataManager shareManage]deleteData:[self.SeleArr[indexPath.row]title_Arr]];//根据model模型里的一个数据删除整个模型的数组数据
    [self.SeleArr removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];

}
@end

Model

#import “Model.h”

@implementation Model
-(NSString *)title_Arr
{
return _title_Arr;
}
@end

猜你喜欢

转载自blog.csdn.net/weixin_43364994/article/details/86555449