3.3 通信录(电话本)---使用FMDB进行数据存储

通信录(电话本)---使用FMDB进行数据存储

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    
    ViewController *vc = [[ViewController alloc] init];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    self.window.rootViewController = nav;
    
    
    return YES;
}
#import "ViewController.h"
#define qfCell @"xiaoqiang"
#import "Person.h"
#import "LQDateBaseManager.h"
@interface ViewController ()
//界面展示
@property (nonatomic,strong) UITableView *tableView;

//存有所有的person模型
@property (nonatomic,strong) NSMutableArray *dataSoure;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     [self setNavigatoionItem];
    [self createTableView];
}
#pragma mark ---导航栏界面展示-----
- (void)setNavigatoionItem{
    
    self.title = @"电话本";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFriend)];
 
}
- (void)addFriend{
    
    //
    [self showAlertWithFriend:nil];
    
}



#pragma mark --- 协议方法---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.dataSoure.count;
    
}
/**实现要显示的数据*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static   NSString *str = @"xiaoqiang";
    
    
    
    // 从复用池中取到自定义的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
        
    }
    
    
    
    //1. 或
    Person *person = self.dataSoure[indexPath.row];
    
    //
    cell.textLabel.text = person.name;
    cell.detailTextLabel.text = person.phone;
   
    
    
    
    return  cell;
}


#pragma mark ----选中回调----

//改
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //改弹窗
    [self showAlertWithFriend:self.dataSoure[indexPath.row]];
    
    
}

#pragma mark ---弹窗展示
- (void)showAlertWithFriend:(Person *)person{
    //如果模型存在,表示修改操作
    
    //如果模型不存在,表示增的操作
    
    UIAlertController *altert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户信息" preferredStyle:UIAlertControllerStyleAlert];
    //添加输入框
    [altert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // 这里写输入框的配置属性
        textField.placeholder =@"请输入姓名";
        
        //输入框文本 可以编辑
        textField.font = [UIFont systemFontOfSize:13];
        
        textField.text = person.name;
        
        
    }];
    
    [altert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // 这里写输入框的配置属性
        textField.placeholder =@"请输入手机号码";
        
        //输入框文本 可以编辑
        textField.font = [UIFont systemFontOfSize:13];
        
        textField.text = person.phone;
        //
//        textField.keyboardType = UIKeyboardTypeNumberPad;
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        
//        [self addAction:altert];
        // 区分不同的情况, 分别调用
        // person如果存在, 表示是改的操作
        // person如果不存在, 表示是增的操作
        if (person) {
            [self updateAction:altert person:person];
        }
        else{
            
            [self addAction:altert];
            
            
        }
        
    }];
    
    [altert addAction:cancel];
    [altert addAction:done];
    
    [self presentViewController:altert animated:YES completion:nil];
    
    
    
    
}

//更新事件,需要多传一个参数
- (void)updateAction:(UIAlertController *)altert person:(Person *)person{
    //改
    //目标:1.界面显示要变, 2. 本地显示要变
    
    //1.界面修改思路
    //1.修改数据源数组,刷新数据源
    
    
    //2.本地修改
    //需要一个新的模型来存储新的内容
    UITextField *usernameText = altert.textFields.firstObject;
    UITextField *phoneText = altert.textFields.lastObject;
    
    Person *newPerson = [[Person alloc] init];
    
    newPerson.name = usernameText.text;
    newPerson.phone = phoneText.text;
    
    /**修改界面显示*/
    //找出一个元素在数组中的下标
    NSInteger index = [self.dataSoure indexOfObject:person];
    
    [self.dataSoure replaceObjectAtIndex:index withObject:newPerson];
    
    //刷新表格
    [self.tableView reloadData];
    
    /**修改本地缓存*/
    [[LQDateBaseManager shareManager] updaeRecord:person withViewRecord:newPerson];
    
}
- (void)addAction:(UIAlertController *) altert{
    
    
    //获取两个输入框的属性
    //   NSString  *username  = altert.textFields.firstObject.text;
    UITextField *usernameText = altert.textFields.firstObject;
    NSString *username = usernameText.text;
    UITextField *telephoneText = altert.textFields.lastObject;
    NSString *telephone = telephoneText.text;
    
    NSLog(@"%@,%@",username,telephone);
    
    //1.把用户信息存到模型里面
    Person *person = [[Person alloc] init];
    person.name = username;
    person.phone = telephone;
    
    
    
    //2.把用户模型存到数组模型中
    
    [self.dataSoure addObject:person];
    
    //3.把数据显示到界面上  刷新表格
    //刷新表格就是冲新走了一步协议方法
    [self.tableView reloadData];
    
    [[LQDateBaseManager shareManager] addRecord:person];
}

- (void)removeTableViewCellAtIndexPath:(Person *) person{
    
    //找出一个元素在数组中的下标
    NSInteger index = [self.dataSoure indexOfObject:person];
    
    [self.dataSoure removeObjectAtIndex:index];
    
    //刷新表格
    [self.tableView reloadData];
    
    [[LQDateBaseManager shareManager] removeRecord:person];
    
}


#pragma mark ---编辑样式---

//1.某一行能否被操作 (默认是YES)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSLog(@"%lu",indexPath.row);
    return  indexPath.row + 1;
    
}


//2.某一行的编辑样式,默认是删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    /**
     UITableViewCellEditingStyleNone, //无样式
     UITableViewCellEditingStyleDelete,//删除样式
     UITableViewCellEditingStyleInsert //添加样式
     */
    
    
    //        return  indexPath.row % 3;
    return UITableViewCellEditingStyleDelete;
    //| 停靠模式
//    return UITableViewCellEditingStyleDelete |UITableViewCellEditingStyleInsert;
}

// 3.编辑样式下的操作,触发的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //editingStyle 编辑的样式
    
    if (editingStyle == UITableViewCellEditingStyleDelete){
    
        [self showRemoveUIAlterController:self.dataSoure[indexPath.row]];
        
        
    }

}


- (void)showRemoveUIAlterController:(Person *) person{
    
    NSString *str = [NSString stringWithFormat:@"是否删除%@这个用户名",person.name];
    
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self removeTableViewCellAtIndexPath:person];
    }];
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler: nil];
    [alter addAction:confirm];
    [alter addAction:done];
    [self presentViewController:alter animated:YES completion:nil];
}

#pragma mark ---数组懒加载---
- (NSMutableArray *)dataSoure{
    if (_dataSoure == nil) {
        _dataSoure = [[NSMutableArray alloc] init];
        
        //读取本地存有的所有数据
        [_dataSoure addObjectsFromArray:[[LQDateBaseManager shareManager] findAll]];
        
//        _dataSoure = [[LQDateBaseManager shareManager] findAll];
    }
    return _dataSoure;

}
- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [self.view addSubview:self.tableView];
    
    //    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:qfCell];
    
}

@end
#import 

@interface Person : NSObject
@property (nonatomic,copy)NSString   *name;
@property (nonatomic,copy)NSString   *phone;
@end

#import "Person.h"

@implementation Person

@end

#import "FMDatabase.h"
@class Person;
@interface LQDateBaseManager : FMDatabase

//一般情况下,一个应用程序里面,有一个数据库就够了
//所以,我们可以把这个数据库设置成一个单例

+ (instancetype)shareManager;
//增
#warning 以下操作,从扩展性来说,传模型会更好些   比如要增添加一些输入框  备注等等
- (void)addRecord:(Person *)person;

//删
- (void)removeRecord:(Person *)person;

//改
- (void)updaeRecord:(Person *)person withViewRecord:(Person *)newRcord;

//  查
- (NSArray *) findAll;

@end
#import "LQDateBaseManager.h"
#import "Person.h"
@implementation LQDateBaseManager
+ (instancetype)shareManager{
    static LQDateBaseManager *_manager = nil;
    
    if (_manager == nil) {
        
        //一:建数据库
        //1.获取沙盒路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES).firstObject stringByAppendingString:@"/data.db"];
        //实例化一个数据库单例
        _manager = [[LQDateBaseManager alloc] initWithPath:path];
        
        //2.打开数据库
        [_manager open];
        
        //二:建表只需一次即可,所以在这里建表
        NSString  *sql = @"create table if not exists Friend (name, phone)";
        
        [_manager executeUpdate:sql];
 
        
    }
    
    return _manager;
    
    
    
//    static  typeof([self class]) instance = nil;
//    if (instance == nil) {
//        
//    }
    
}


//添加一条数据
- (void)addRecord:(Person *)person{
    
    //添加语句
    
    NSString *sql =@"insert into Friend (name, phone) values (?, ?)";
    [self executeUpdate:sql,person.name,person.phone];
    
    
}
- (void)removeRecord:(Person *)person{
    
    NSString *sql =  @"delete from Friend where name = ? and phone = ?";
    
    [self executeUpdate:sql,person.name,person.phone];
    
    
    
}

- (void)updaeRecord:(Person *)person withViewRecord:(Person *)newRcord{
    
    NSString *sql = @"update Friend set name = ?, phone = ? where name =? and phone =?";
//    where name = ? and phone = ?
    
    [self executeUpdate:sql,newRcord.name,newRcord.phone,person.name,person.phone];
    
    
}

- (NSArray *)findAll{
    NSString *sql = @"select * from Friend";
    
    //实例化一个可变数组,用来存储所有的查询结果
    NSMutableArray *allResults = [NSMutableArray array];
    //执行查询
  FMResultSet *set =  [self executeQuery:sql];
    while (set.next) {
        
        //把每一条数据封装为一个模型
        
        Person * person = [[Person alloc] init];
        
        person.name =  [set objectForColumnName:@"name"];
        person.phone = [set objectForColumnIndex:1];
        
        //把封装好的模型添加到数组中
        [allResults addObject:person];
    }
    
    return allResults;
}
@end
通信录(电话本)---使用FMDB进行数据存储.png

你可能感兴趣的:(3.3 通信录(电话本)---使用FMDB进行数据存储)