FMDB

//

//? AppDelegate.h

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import

#import

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property(readonly,strong)NSPersistentContainer*persistentContainer;

- (void)saveContext;

@end


//

//? AppDelegate.m

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

? ? // Override point for customization after application launch.

? ? return YES;

}

- (void)applicationWillResignActive:(UIApplication*)application {

? ? // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

? ? // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication*)application {

? ? // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

? ? // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication*)application {

? ? // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication*)application {

? ? // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

? ? // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

? ? // Saves changes in the application's managed object context before the application terminates.

? ? [selfsaveContext];

}

#pragma mark - Core Data stack

@synthesizepersistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {

? ? // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.

? ? @synchronized (self) {

? ? ? ? if(_persistentContainer ==nil) {

? ? ? ? ? ? _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"______1"];

? ? ? ? ? ? [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {

? ? ? ? ? ? ? ? if(error !=nil) {

? ? ? ? ? ? ? ? ? ? // Replace this implementation with code to handle the error appropriately.

? ? ? ? ? ? ? ? ? ? // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.


? ? ? ? ? ? ? ? ? ? /*

?? ? ? ? ? ? ? ? ? ? Typical reasons for an error here include:

?? ? ? ? ? ? ? ? ? ? * The parent directory does not exist, cannot be created, or disallows writing.

?? ? ? ? ? ? ? ? ? ? * The persistent store is not accessible, due to permissions or data protection when the device is locked.

?? ? ? ? ? ? ? ? ? ? * The device is out of space.

?? ? ? ? ? ? ? ? ? ? * The store could not be migrated to the current model version.

?? ? ? ? ? ? ? ? ? ? Check the error message to determine what the actual problem was.

? ? ? ? ? ? ? ? ? ? */

? ? ? ? ? ? ? ? ? ? NSLog(@"Unresolved error %@, %@", error, error.userInfo);

? ? ? ? ? ? ? ? ? ? abort();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }];

? ? ? ? }

? ? }


? ? return_persistentContainer;

}

#pragma mark - Core Data Saving support

- (void)saveContext {

? ? NSManagedObjectContext *context =self.persistentContainer.viewContext;

? ? NSError *error =nil;

? ? if([context hasChanges] && ![context save:&error]) {

? ? ? ? // Replace this implementation with code to handle the error appropriately.

? ? ? ? // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

? ? ? ? NSLog(@"Unresolved error %@, %@", error, error.userInfo);

? ? ? ? abort();

? ? }

}

@end



//

//? Model.h

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interface Model : NSObject

@property(nonatomic,assign)int ID;

@property (nonatomic,strong)NSString *imgsrc,*title;

@end

NS_ASSUME_NONNULL_END




//

//? Model.m

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import "Model.h"

@implementation Model

- (void)setValue:(id)value forUndefinedKey:(NSString*)key{


}

@end


//

//? DataModel.h

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interfaceDataModel :NSObject

+(DataModel*)sharedDataHandle;

//增

-(void)addOneMovie:(id)movie;

//刪

-(void)deleteMovieByID:(int)delID;

//查

-(id)getAllMovies;

@end

NS_ASSUME_NONNULL_END




//

//? DataModel.m

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import "DataModel.h"

#import"FMDB.h"

#import "Model.h"

staticDataModel*_defaulthandle =nil;

@interface DataModel ()

@property(nonatomic,strong)FMDatabase*fMDB;? //

@end

@implementation DataModel

+(DataModel*)sharedDataHandle

{

? ? if (_defaulthandle == nil) {

? ? ? ? _defaulthandle = [[DataModel alloc] init];


? ? }

? ? return _defaulthandle;

}

+(instancetype)allocWithZone:(struct_NSZone*)zone

{

? ? if (_defaulthandle == nil) {

? ? ? ? _defaulthandle= [superallocWithZone:zone];

? ? }

? ? return _defaulthandle;

}

-(FMDatabase *)fMDB

{

? ? if(!_fMDB) {

? ? ? ? NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"movies.sqlite"];

? ? ? ? NSLog(@"path=====%@",path);


? ? ? ? _fMDB= [FMDatabasedatabaseWithPath:path];

? ? ? ? [selfinitTable];

? ? }

? ? return _fMDB;

}

// 初始化數(shù)據(jù)表

-(void)initTable

{

? ? [_fMDBopen];


? ? [_fMDB executeUpdate:@"CREATE TABLE movie (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,imgsrc TEXT)"];



? ? [_fMDBclose];

}

-(void)addOneMovie:(Model*)movie

{

? ? [self.fMDBopen];


? ? // [self.fMDB executeUpdate:@"insert into movie (name,actor,date,price) values(?,?,?,?)",movie.name,movie.actor ,movie.date ,movie.price];


? ? [self.fMDB executeUpdateWithFormat:@"insert into movie (title,imgsrc) values(%@,%@)",movie.title,movie.imgsrc ];


? ? [self.fMDBclose];

}

-(void)deleteMovieByID:(int)delID

{

? ? [self.fMDBopen];


? ? [self.fMDB executeUpdateWithFormat:@"DELETE FROM movie WHERE id = %d",delID];


? ? [self.fMDBclose];

}

-(NSArray*)getAllMovies

{

? ? [self.fMDBopen];


? ? NSMutableArray *arr = [[NSMutableArray alloc] init];


? ? FMResultSet *result = [self.fMDB executeQuery:@"SELECT * FROM movie"];

? ? while([resultnext])

? ? {

? ? ? ? Model*movie = [[Modelalloc]init];

? ? ? ? [arraddObject:movie];


? ? ? ? movie.ID= [resultintForColumnIndex:0];

? ? ? ? movie.title= [resultstringForColumnIndex:1];

? ? ? ? movie.imgsrc= [resultstringForColumnIndex:2];


? ? }

? ? [self.fMDBclose];


? ? return[arrcopy];

}

@end




//

//? ZDYTableViewCell.h

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import

#import "Model.h"

#import "DataModel.h"

NS_ASSUME_NONNULL_BEGIN

@interfaceZDYTableViewCell :UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imgv;

@property (weak, nonatomic) IBOutlet UILabel *lable;

- (void)loadData:(Model*)mod;

@end

NS_ASSUME_NONNULL_END





//

//? ZDYTableViewCell.m

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import "ZDYTableViewCell.h"

@implementationZDYTableViewCell

- (void)loadData:(Model*)mod{

? ? if(mod) {

? ? ? ? self.lable.text=mod.title;

? ? ? ? self.imgv.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mod.imgsrc]]];

? ? }


}

- (void)awakeFromNib {

? ? [super awakeFromNib];

? ? // Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

? ? [supersetSelected:selectedanimated:animated];

? ? // Configure the view for the selected state

}

@end




//

//? ViewController.h

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import

@interfaceViewController :UIViewController

@end




//

//? ViewController.m

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import "ViewController.h"

#import "Model.h"

#import "AFNetworking.h"

#import "ZDYTableViewCell.h"

#import "DataModel.h"

#import "xqViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *table;

@property (nonatomic,strong)NSMutableArray *data;

- (IBAction)gotoxq:(id)sender;

@end

staticNSString*oj =@"cell";

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];


//? ? [self.table registerClass:[ZDYTableViewCell class] forCellReuseIdentifier:oj];

? ? [self.table registerNib:[UINib nibWithNibName:@"ZDYTableViewCell" bundle:nil] forCellReuseIdentifier:oj];


? ? self.data=[[NSMutableArray alloc]init];


? ? [self CreateAFNet];


? ? [self.viewaddSubview:self.table];


? ? NSArray *allData = [[DataModel sharedDataHandle] getAllMovies];

?? ? _data= [NSMutableArrayarrayWithArray:allData];

? ? [self.table reloadData];

}

- (UITableView*)table{

? ? if(!_table) {

? ? ? ? _table=[[UITableView alloc]initWithFrame:self.view.frame];

? ? ? ? _table.delegate=self;

? ? ? ? _table.dataSource=self;

? ? }


? ? return _table;

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

? ? return self.data.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? ZDYTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];

? ? if(self.data.count>0) {

? ? ? ? Model*mod =self.data[indexPath.row];

? ? ? ? [cellloadData:mod];




? ? }

? ? returncell;

}

- (void)CreateAFNet{

? ? //? return;

? ? AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];

? ? [managerGET:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html" parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {

? ? }success:^(NSURLSessionDataTask*_Nonnulltask,id? _NullableresponseObject) {

? ? ? ? NSLog(@"%@",responseObject);


? ? ? ? for(NSDictionary*dicinresponseObject[@"T1348647853363"]) {

? ? ? ? ? ? Model*mod=[[Modelalloc]init];

? ? ? ? ? ? [modsetValuesForKeysWithDictionary:dic];

? ? ? ? ? ? [self.dataaddObject:mod];

? ? ? ? }


? ? ? ? [self.tablereloadData];

? ? }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

? ? ? ? NSLog(@"%@",error);


? ? }];

}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? return 60;

}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {


? ? Model*mod =self.data[indexPath.row];

?? [[DataModel sharedDataHandle] addOneMovie:mod];

? ? NSLog(@"添加了第%d行的數(shù)據(jù)",indexPath.row);


}

- (IBAction)gotoxq:(id)sender {

? ? xqViewController*xq=[xqViewController new];

? ? [self.navigationController pushViewController:xq animated:YES];

}

@end





//

//? xqViewController.h

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interfacexqViewController :UIViewController

@end

NS_ASSUME_NONNULL_END






//

//? xqViewController.m

//? 靠前瞎雞兒信1

//

//? Created by 徐才茵 on 20/12/2018.

//? Copyright ? 2018 徐才茵. All rights reserved.

//

#import "xqViewController.h"

#import "DataModel.h"

#import "Model.h"

#import "UIImageView+WebCache.h"

@interface xqViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView*table;

@property(nonatomic,strong)NSMutableArray*array;

@end

@implementationxqViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? _table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

? ? _table.delegate=self;

? ? _table.dataSource=self;

? ? [self.view addSubview:_table];


? ? _array = [[DataModel sharedDataHandle] getAllMovies];

? ? [_table reloadData];

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

? ? return_array.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? staticNSString*oj=@"cell";

? ? UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:oj];

? ? if(!cell) {

? ? ? ? cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:oj];

? ? }


? ? Model*s? = [_arrayobjectAtIndex:indexPath.row];

? ? cell.textLabel.text= s.title;

? ? [cell.imageViewsd_setImageWithURL:[NSURLURLWithString:s.imgsrc]];

? ? returncell;

}

-(BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {

? ? return YES;

}

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


? ? Model*mod =self.array[indexPath.row];

? ? [[DataModel sharedDataHandle] deleteMovieByID:mod.ID];


? ? _array = [[DataModel sharedDataHandle] getAllMovies];

? ? [_table reloadData];


}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末济瓢,一起剝皮案震驚了整個濱河市谒麦,隨后出現(xiàn)的幾起案子樱蛤,更是在濱河造成了極大的恐慌频鉴,老刑警劉巖缺厉,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件永高,死亡現(xiàn)場離奇詭異,居然都是意外死亡提针,警方通過查閱死者的電腦和手機命爬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辐脖,“玉大人饲宛,你說我怎么就攤上這事∈燃郏” “怎么了艇抠?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長久锥。 經(jīng)常有香客問我家淤,道長,這世上最難降的妖魔是什么瑟由? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任絮重,我火速辦了婚禮,結(jié)果婚禮上歹苦,老公的妹妹穿的比我還像新娘青伤。我一直安慰自己,他們只是感情好殴瘦,可當(dāng)我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布狠角。 她就那樣靜靜地躺著,像睡著了一般蚪腋。 火紅的嫁衣襯著肌膚如雪丰歌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天屉凯,我揣著相機與錄音动遭,去河邊找鬼。 笑死神得,一個胖子當(dāng)著我的面吹牛厘惦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼宵蕉,長吁一口氣:“原來是場噩夢啊……” “哼酝静!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起羡玛,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤别智,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后稼稿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體薄榛,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年让歼,在試婚紗的時候發(fā)現(xiàn)自己被綠了敞恋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡谋右,死狀恐怖硬猫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情改执,我是刑警寧澤啸蜜,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站辈挂,受9級特大地震影響衬横,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜终蒂,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一蜂林、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧后豫,春花似錦悉尾、人聲如沸突那。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽愕难。三九已至早龟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間猫缭,已是汗流浹背葱弟。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留猜丹,地道東北人芝加。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像射窒,于是被迫代替她去往敵國和親藏杖。 傳聞我的和親對象是個殘疾皇子将塑,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 用cocoaPods配置第三方文件 第一步。打開終端 第二步蝌麸。cd+文件夾 第三步点寥。pod init 第四步。打開...
    不說謊的匹諾曹Y閱讀 1,090評論 0 1
  • 哦吼吼来吩,又研究了幾天敢辩,把FMDB這個封裝好的數(shù)據(jù)庫搞定了,寫了個簡單的例子弟疆,基于FMDB的添刪改查操作戚长,界面很一般...
    lichengjin閱讀 531評論 0 0
  • 作者唯一QQ:228544117。兽间。历葛。。嘀略。 =========后面的都要新建一個文章 AppDelegate.h ...
    CC_iOS閱讀 877評論 0 0
  • //聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄 /**注意點: 1.看 GIF 效果圖.2.看連線...
    石虎132閱讀 747評論 0 12
  • 轉(zhuǎn)載別人的文章 //聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄 /** 注意點: 1.看 GIF ...
    Whatever永不放棄閱讀 965評論 0 0