先导入fmdb库
修改跟视图
在LoadData.h文件中
#import <Foundation/Foundation.h>
#import "User.h"
@interface LoadData : NSObject <NSCopying,NSMutableCopying>
//分享单例对象
+ (instancetype)shareLoadData;
//增加数据
- (void)insertData:(User *)usr;
//获取所有数据
- (NSArray *)queryData;
//删除数据
- (void)deleteData:(User *)usr;
@end
LoadData.m中
#import "LoadData.h"
#import "FMDB.h"
@interface LoadData()
{
//定义数据库指针
FMDatabase *db;
}
//创建数据库
- (void)createDataBase;
//创建数据表
- (void)createTable;
//关闭数据库
- (void)closeDataBase;
@end
//定义静态全局变量
static LoadData *ld;
@implementation LoadData
//分享单例对象
+ (instancetype)shareLoadData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ld = [[LoadData alloc]init];
});
return ld;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!ld) {
ld = [[super allocWithZone:zone]init];
}
return ld;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return self;
}
//创建数据库
- (void)createDataBase
{
//获取数据库路径
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbPath = [[arr lastObject]stringByAppendingPathComponent:@"data.db"];
//创建数据库
db = [FMDatabase databaseWithPath:dbPath];
}
//创建数据表
- (void)createTable
{
//打开数据库
[db open];
//创建数据表
NSString *sql = @"create table if not exists UserTable(id integer primary key autoincrement,phoneNum integer,password integer,name text)";
[db executeUpdate:sql];
//关闭数据库
[self closeDataBase];
}
//关闭数据库
- (void)closeDataBase
{
[db close];
}
//增加数据
- (void)insertData:(User *)usr
{
//打开数据库
[db open];
//添加数据
NSString *sql = [NSString stringWithFormat:@"insert into UserTable(phoneNum,password,name) values('%ld','%ld','%@')",usr.phoneNum,usr.password,usr.name];
[db executeUpdate:sql];
//关闭数据库
[self closeDataBase];
}
//获取所有数据
- (NSArray *)queryData
{
//创建数据库
[self createDataBase];
//创建数据表
[self createTable];
//打开数据库
[db open];
//获取所有数据
NSMutableArray *mArr = [NSMutableArray array];
NSString *sql = @"select * from UserTable";
FMResultSet *set =[db executeQuery:sql];
while ([set next]) {
User *usr = [[User alloc]init];
usr.idNum = [[set stringForColumn:@"id"]integerValue];
usr.phoneNum = [[set stringForColumn:@"phoneNum"]integerValue];
usr.password = [[set stringForColumn:@"password"]integerValue];
usr.name = [set stringForColumn:@"name"];
[mArr addObject:usr];
}
//关闭数据库
[self closeDataBase];
return [mArr copy];
}
//删除数据
- (void)deleteData:(User *)usr
{
//打开数据库
[db open];
//删除数据
NSString *sql = [NSString stringWithFormat:@"delete from UserTable where id = '%ld'",usr.idNum];
[db executeUpdate:sql];
//关闭数据库
[self closeDataBase];
}
@end
在ViewController.m中
#import "ViewController.h"
#import "LoadData.h"
#import "User.h"
#import "AddViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
//定义变量数组、表格
NSArray *arr;
UITableView *table;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航标题
self.navigationItem.title = @"全部用户";
//创建添加按钮
UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(itemClicked)];
self.navigationItem.rightBarButtonItem = addItem;
//初始化表格
table = [[UITableView alloc]initWithFrame:self.view.bounds];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
}
- (void)viewWillAppear:(BOOL)animated
{
//获取全部数据
arr = [[LoadData shareLoadData]queryData];
//刷新表格
[table reloadData];
}
//设置导航按钮响应方法
- (void)itemClicked
{
//跳转
AddViewController *avc = [[AddViewController alloc]init];
[self.navigationController pushViewController:avc animated:YES];
}
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
//设置单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
User *usr = arr[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"id:%ld---phone:%ld",usr.idNum,usr.phoneNum];
cell.detailTextLabel.text = [NSString stringWithFormat:@"name:%@---pwd:%ld",usr.name,usr.password];
return cell;
}
//设置删除单元格响应方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除数据
[[LoadData shareLoadData]deleteData:arr[indexPath.row]];
//重新获取全部数据
arr = [[LoadData shareLoadData]queryData];
//刷新表格
[table reloadData];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在AddViewController.m
#import "AddViewController.h"
#import "LoadData.h"
#import "User.h"
@interface AddViewController ()
{
//定义变量手机号、密码、姓名文本框
UITextField *phoneNumTF,*passwordTF,*nameTF;
}
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSArray *arr = @[@"手机号:",@"密码:",@"姓名:"];
for (int i = 0,y = 90; i < 3; i ++) {
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(20, y, 80, 44)];
lab.text = arr[i];
[self.view addSubview:lab];
y += 60;
}
//初始化手机号文本框
phoneNumTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 90, 200, 44)];
phoneNumTF.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:phoneNumTF];
//初始化密码文本框
passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 200, 44)];
passwordTF.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:passwordTF];
//初始化姓名文本框
nameTF = [[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 44)];
nameTF.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:nameTF];
//创建按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(180, 300, 60, 44)];
[btn setTitle:@"提交" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//设置按钮响应方法
- (void)btnClicked
{
//加入正则表达式判断
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:phoneNumTF.text] == YES) || ([regextestcm evaluateWithObject:phoneNumTF.text] == YES) || ([regextestct evaluateWithObject:phoneNumTF.text] == YES) || ([regextestcu evaluateWithObject:phoneNumTF.text] == YES)){
//插入数据
User *usr = [[User alloc]init];
usr.phoneNum = [phoneNumTF.text integerValue];
usr.password = [passwordTF.text integerValue];
usr.name = nameTF.text;
[[LoadData shareLoadData]insertData:usr];
[self.navigationController popViewControllerAnimated:YES];
}else{
//提示
UIAlertController *alc = [UIAlertController alertControllerWithTitle:@"警告" message:@"手机号码不正确!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *act = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
[alc addAction:act];
[self presentViewController:alc animated:YES completion:nil];
}
}
@end
在User.h中
#import <Foundation/Foundation.h>
@interface User : NSObject
//定义属性ID、手机号、密码、姓名
@property (nonatomic,assign)NSInteger idNum,phoneNum,password;
@property (nonatomic,strong)NSString *name;
@end
在User.m中
#import "User.h"
@implementation User
@end