知乎日报第四周总结

这周基本完成了知乎日报的内容,主要写了点赞收藏的滑动刷新的保存,还有收藏文章的功能,主要用到FMDB库。

对于如何使用FMDB库

同样需要用cocopads引入FMDB库,使用时需要引入头文件

#import "FMDatabase.h"

FMDB库的使用类似于C语言的文件操作,需要我们手动创库,之后编译器会生成一个文件用于数据的存储,当程序关闭后,文件中的内容也不会消失,当程序重新启动后,会继续利用从而达到本地持久化。

FMDB的创建

- (void)dateBaseInit {
        NSString* collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     
        NSString* filename = [collectionDoc stringByAppendingPathComponent:@"collection1.sqlite"];
        
        self.collectionDataBase = [FMDatabase databaseWithPath:filename];
  
        if ([self.collectionDataBase open]) {
            BOOL result = [self.collectionDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS collectionData (mainLabel text NOT NULL, imageURL text NOT NULL, id text NOT NULL, url text NOT NULL);"];
            if (result) {
                NSLog(@"创表成功");
            } else {
                NSLog(@"创表失败");
            }
        }


        NSString *goodDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString * fileName = [goodDoc stringByAppendingPathComponent:@"likes.sqlite"];
        self.likesDataBase = [FMDatabase databaseWithPath:fileName];
        if ([self.likesDataBase open]) {
            BOOL result = [self.likesDataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS likesData (id text NOT NULL);"];
            if (result) {
                NSLog(@"创表成功");
            } else {
                NSLog(@"创表失败");
            }
        }
}

不必在意其何时创建的,只要在你用到它时创建好就行。在程序重新启动时,该段代码会在执行一遍,但不会创建一个新的库覆盖前面的,可以理解为会执行但没有任何效果。

数据的添加,删除,查询。

- (void)insertLikesData:(NSString*)string {
    
    if ([self.likesDataBase open]) {
        BOOL result = [self.likesDataBase executeUpdate:@"INSERT INTO likesData (id) VALUES (?);", string];
        if (!result) {
            NSLog(@"增加数据失败");
        } else {
            NSLog(@"增加数据成功");
        }
        [self.likesDataBase close];
    }
}
- (void)deleteLikesDate:(NSString*)nowID {
    if ([self.likesDataBase open]) {
        NSString *sql = @"delete from likesData WHERE id = ?";
        BOOL result = [self.likesDataBase executeUpdate:sql, nowID];
            if (!result) {
                NSLog(@"数据删除失败");
            } else {
                NSLog(@"数据删除成功");
            }
            [self.likesDataBase close];
        }
}
NSString* collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   
    NSString* filename = [collectionDoc stringByAppendingPathComponent:@"collection1.sqlite"];
    
    self.collectionDataBase = [FMDatabase databaseWithPath:filename];
    if ([self.collectionDataBase open]) {
        FMResultSet* collectionResultSet = [self.collectionDataBase executeQuery:@"SELECT * FROM collectionData"];
        while ([collectionResultSet next]) {
           
            NSString* idString = [collectionResultSet stringForColumn:@"id"];
        
            if ([nowIdSting isEqualToString:idString]) {
                self.flage1 = 1;
                self.keepButton.tag  = 6;
                [self.keepButton setImage:[UIImage imageNamed:@"shoucangxuanzhong-2"] forState:UIControlStateNormal];
            }
        }
        [self.collectionDataBase close];
    }

对于在不同的文件下获取到库,在开始没有理解其用处,以为要将库保存下来并传值,那么就没有必要创这个库。以上面的查询为例,获取数据库文件的路径,获取数据库,注意文件的路径必须一样。

NSString* collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   
    NSString* filename = [collectionDoc stringByAppendingPathComponent:@"collection1.sqlite"];
    
    self.collectionDataBase = [FMDatabase databaseWithPath:filename];

对于点赞收藏的滑动刷新

获取该文章的ID,遍历数据库判断是否收藏点赞,同时刷新控件,这里要注意要改变按钮的点击状态(这里是改变按钮的tag值)

- (void)likesAndCollectReload:(NSString*) nowIdSting{
    self.flage1 = 0;
    self.flage2 = 0;
    NSString* collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   
    NSString* filename = [collectionDoc stringByAppendingPathComponent:@"collection1.sqlite"];
    
    self.collectionDataBase = [FMDatabase databaseWithPath:filename];
    if ([self.collectionDataBase open]) {
        FMResultSet* collectionResultSet = [self.collectionDataBase executeQuery:@"SELECT * FROM collectionData"];
        while ([collectionResultSet next]) {
           
            NSString* idString = [collectionResultSet stringForColumn:@"id"];
        
            if ([nowIdSting isEqualToString:idString]) {
                self.flage1 = 1;
                self.keepButton.tag  = 6;
                [self.keepButton setImage:[UIImage imageNamed:@"shoucangxuanzhong-2"] forState:UIControlStateNormal];
            }
        }
        [self.collectionDataBase close];
    }
    if (self.flage1 == 0) {
         self.keepButton.tag  = 3;
         [self.keepButton setImage:[UIImage imageNamed:@"shoucang-2"] forState:UIControlStateNormal];
    }
    
    NSString *goodDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString * fileName = [goodDoc stringByAppendingPathComponent:@"likes.sqlite"];
        self.likesDataBase = [FMDatabase databaseWithPath:fileName];
    if ([self.likesDataBase open]) {
        // 1.执行查询语句
        FMResultSet *likesResultSet = [self.likesDataBase executeQuery:@"SELECT * FROM likesData"];
        // 2.遍历结果
        while ([likesResultSet next]) {
            NSString* idString1 = [likesResultSet stringForColumn:@"id"];
            if ([nowIdSting isEqualToString:idString1]) {
                self.flage2 = 1;
                self.likesButton.tag = 4;
                [self.likesButton setImage:[UIImage imageNamed:@"a-24geshangchuan-20"] forState:UIControlStateNormal];
            }
        }
        [self.likesDataBase close];
    }
    if (self.flage2 == 0) {
             self.likesButton.tag = 2;
            [self.likesButton setImage:[UIImage imageNamed:@"good"] forState:UIControlStateNormal];
    }
}

对于文章的收藏于取消收藏,我们一般将数据库的数据提取到数组中使用,在数据库内容发生改变时,就需要重新提取。以在文章内容界面取消收藏,返回收藏夹更新收藏夹的内容为例子。这里用到通知在返回到收藏夹执行相应的更新方法

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadCollectCell" object:nil userInfo:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
- (void)reloadCollectCell{
    [self twoArrayInit];
    [self.storiesTableView reloadData];
}
- (void)twoArrayInit {
    self.titleArray = [NSMutableArray array];
    self.imageUrlArray = [NSMutableArray array];
    self.idArray = [NSMutableArray array];
    self.urlArray = [NSMutableArray array];
    NSString* collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString* filename = [collectionDoc stringByAppendingPathComponent:@"collection1.sqlite"];
    
    self.collectionDataBase = [FMDatabase databaseWithPath:filename];
    if ([self.collectionDataBase open]) {
        FMResultSet* collectionResultSet = [self.collectionDataBase executeQuery:@"SELECT * FROM collectionData"];
        while ([collectionResultSet next]) {
            NSString* idString = [collectionResultSet stringForColumn:@"id"];
            [self.idArray addObject:idString];
            [self.titleArray addObject:[collectionResultSet stringForColumn:@"mainLabel"]];
            [self.imageUrlArray addObject:[collectionResultSet stringForColumn:@"imageURL"]];
            [self.urlArray addObject:[collectionResultSet stringForColumn:@"url"]];
        }
        [self.collectionDataBase close];
    }
}

你可能感兴趣的:(ios,objective-c,xcode)