先導(dǎo)入fmdb庫
修改跟視圖
在LoadData.h文件中
#import <Foundation/Foundation.h>
#import "User.h"
@interface LoadData : NSObject <NSCopying,NSMutableCopying>
//分享單例對象
+ (instancetype)shareLoadData;
//增加數(shù)據(jù)
- (void)insertData:(User *)usr;
//獲取所有數(shù)據(jù)
- (NSArray *)queryData;
//刪除數(shù)據(jù)
- (void)deleteData:(User *)usr;
@end
LoadData.m中
#import "LoadData.h"
#import "FMDB.h"
@interface LoadData()
{
//定義數(shù)據(jù)庫指針
FMDatabase *db;
}
//創(chuàng)建數(shù)據(jù)庫
- (void)createDataBase;
//創(chuàng)建數(shù)據(jù)表
- (void)createTable;
//關(guān)閉數(shù)據(jù)庫
- (void)closeDataBase;
@end
//定義靜態(tài)全局變量
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;
}
//創(chuàng)建數(shù)據(jù)庫
- (void)createDataBase
{
//獲取數(shù)據(jù)庫路徑
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbPath = [[arr lastObject]stringByAppendingPathComponent:@"data.db"];
//創(chuàng)建數(shù)據(jù)庫
db = [FMDatabase databaseWithPath:dbPath];
}
//創(chuàng)建數(shù)據(jù)表
- (void)createTable
{
//打開數(shù)據(jù)庫
[db open];
//創(chuàng)建數(shù)據(jù)表
NSString *sql = @"create table if not exists UserTable(id integer primary key autoincrement,phoneNum integer,password integer,name text)";
[db executeUpdate:sql];
//關(guān)閉數(shù)據(jù)庫
[self closeDataBase];
}
//關(guān)閉數(shù)據(jù)庫
- (void)closeDataBase
{
[db close];
}
//增加數(shù)據(jù)
- (void)insertData:(User *)usr
{
//打開數(shù)據(jù)庫
[db open];
//添加數(shù)據(jù)
NSString *sql = [NSString stringWithFormat:@"insert into UserTable(phoneNum,password,name) values('%ld','%ld','%@')",usr.phoneNum,usr.password,usr.name];
[db executeUpdate:sql];
//關(guān)閉數(shù)據(jù)庫
[self closeDataBase];
}
//獲取所有數(shù)據(jù)
- (NSArray *)queryData
{
//創(chuàng)建數(shù)據(jù)庫
[self createDataBase];
//創(chuàng)建數(shù)據(jù)表
[self createTable];
//打開數(shù)據(jù)庫
[db open];
//獲取所有數(shù)據(jù)
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];
}
//關(guān)閉數(shù)據(jù)庫
[self closeDataBase];
return [mArr copy];
}
//刪除數(shù)據(jù)
- (void)deleteData:(User *)usr
{
//打開數(shù)據(jù)庫
[db open];
//刪除數(shù)據(jù)
NSString *sql = [NSString stringWithFormat:@"delete from UserTable where id = '%ld'",usr.idNum];
[db executeUpdate:sql];
//關(guān)閉數(shù)據(jù)庫
[self closeDataBase];
}
@end
在ViewController.m中
#import "ViewController.h"
#import "LoadData.h"
#import "User.h"
#import "AddViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
//定義變量數(shù)組、表格
NSArray *arr;
UITableView *table;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//設(shè)置導(dǎo)航標(biāo)題
self.navigationItem.title = @"全部用戶";
//創(chuàng)建添加按鈕
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
{
//獲取全部數(shù)據(jù)
arr = [[LoadData shareLoadData]queryData];
//刷新表格
[table reloadData];
}
//設(shè)置導(dǎo)航按鈕響應(yīng)方法
- (void)itemClicked
{
//跳轉(zhuǎn)
AddViewController *avc = [[AddViewController alloc]init];
[self.navigationController pushViewController:avc animated:YES];
}
//設(shè)置行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
//設(shè)置單元格內(nèi)容
- (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;
}
//設(shè)置刪除單元格響應(yīng)方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//刪除數(shù)據(jù)
[[LoadData shareLoadData]deleteData:arr[indexPath.row]];
//重新獲取全部數(shù)據(jù)
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 ()
{
//定義變量手機(jī)號(hào)、密碼、姓名文本框
UITextField *phoneNumTF,*passwordTF,*nameTF;
}
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSArray *arr = @[@"手機(jī)號(hào):",@"密碼:",@"姓名:"];
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;
}
//初始化手機(jī)號(hào)文本框
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];
//創(chuàng)建按鈕
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];
}
//設(shè)置按鈕響應(yīng)方法
- (void)btnClicked
{
//加入正則表達(dá)式判斷
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)){
//插入數(shù)據(jù)
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:@"手機(jī)號(hào)碼不正確!" 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、手機(jī)號(hào)、密碼、姓名
@property (nonatomic,assign)NSInteger idNum,phoneNum,password;
@property (nonatomic,strong)NSString *name;
@end
在User.m中
#import "User.h"
@implementation User
@end