【iOS】—— 仿写知乎日报第三周总结

知乎日报

这周有点小摆,不过还是基本完成了知乎日报的仿写任务,这周的主要重心在完成评论界面和FMDB数据库存储的使用以及离线加载。请添加图片描述

请添加图片描述

请添加图片描述

评论

在写评论界面的时候,首先是关于评论文字高度的问题,刚开始使用的方法用法不对,所以一直无法将label的高度获取下来,导致刚开始写的评论高度都是固定的,再后来仔细学习了

sizeThatFits:

这个方法的用法,发现这个方法必须在一个label的基础上才能用,然后我把这个函数在tableView的事件函数上使用,会发生奇奇怪怪的复用问题,因此我把这个方法放在了tableView初始化之前:

UILabel *label = [[UILabel alloc] init];
label.text = self.longDictionary[@"comments"][i][@"content"];
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:18];
//sizeThatFits:方法的使用
[self.longLabelHeightArray addObject:[NSString stringWithFormat:@"%lf", [label sizeThatFits:CGSizeMake(0.89 * SIZE_WIDTH - 50, CGFLOAT_MAX)].height + 35]];

对于正常的没有回复的评论按照这样写就可以解决问题,但是遇到有回复的就需要判断回复的评论是小于两行还是大于两行,如果小于两行直接放在主label的下面,大于两行则将其只显示两行,然后下面显示展开button,对于是否有button在tableView事件函数里进行判定,如果大于两行有button,其余情况也有button,但是颜色为clear,并且只有当button颜色不是clear时候才能触发事件函数。

//展开button事件函数
- (void)pressExpand:(UIButton *)button {
    if (button.tintColor != [UIColor clearColor]) {
        if ([self.allBoolArray[button.tag] isEqualToString: @"0"]) {
            if ([self.longDictionary[@"comments"] count] == 0) {
                self.shortLabelHeightArray[button.tag] = [NSString stringWithFormat:@"%lf", [self.shortBoolArray[button.tag] doubleValue] + [self.shortLabelHeightArray[button.tag] doubleValue]];
            } else {
                if (button.tag < [self.longDictionary[@"comments"] count]) {
                    self.longLabelHeightArray[button.tag] = [NSString stringWithFormat:@"%lf", [self.longBoolArray[button.tag] doubleValue] + [self.longLabelHeightArray[button.tag] doubleValue]];
                } else {
                    self.shortLabelHeightArray[button.tag - [_longDictionary[@"comments"] count]] = [NSString stringWithFormat:@"%lf", [self.shortBoolArray[button.tag - [self.longDictionary[@"comments"] count]] doubleValue] + [self.shortLabelHeightArray[button.tag - [self.longDictionary[@"comments"] count]] doubleValue]];
                }   
            }
            self.allBoolArray[button.tag] = @"1";
        } else {
            if ([self.longDictionary[@"comments"] count] == 0) {
                self.shortLabelHeightArray[button.tag] = [NSString stringWithFormat:@"%lf", -[self.shortBoolArray[button.tag] doubleValue] + [self.shortLabelHeightArray[button.tag] doubleValue]];
            } else {
                if (button.tag < [self.longDictionary[@"comments"] count]) {
                    self.longLabelHeightArray[button.tag] = [NSString stringWithFormat:@"%lf", -[self.longBoolArray[button.tag] doubleValue] + [self.longLabelHeightArray[button.tag] doubleValue]];
                } else {
                    self.shortLabelHeightArray[button.tag - [self.longDictionary[@"comments"] count]] = [NSString stringWithFormat:@"%lf", -[self.shortBoolArray[button.tag - [self.longDictionary[@"comments"] count]] doubleValue] + [self.shortLabelHeightArray[button.tag - [self.longDictionary[@"comments"] count]] doubleValue]];
                }
            }
            self.allBoolArray[button.tag] = @"0";
        }
        [self.tableView reloadData];
    }
}

FMDB存储

关于FMDB存储类似于c语言的文件操作,但是用法比文件操作简单很多,主要操作分为FMDatabase数据库的初始化和FMDatabase的增删改查,理解抽象但是操作比较简单易懂。

可以看大佬博客
FMDB的基础用法

这是我的关于收藏和点赞数据库的基本操作:

//FMDB初始化
- (void)databaseInit {
    NSString *collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *collectionFileName = [collectionDoc stringByAppendingPathComponent:@"collectionData.sqlite"];
    self.collectionDatabase = [FMDatabase databaseWithPath:collectionFileName];
    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);"];
        if (result) {
            NSLog(@"创表成功");
        } else {
            NSLog(@"创表失败");
        }
    }
    
    NSString *goodDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *goodFileName = [goodDoc stringByAppendingPathComponent:@"goodData.sqlite"];
    self.goodDatabase = [FMDatabase databaseWithPath:goodFileName];
    if ([self.goodDatabase open]) {
        BOOL result = [self.goodDatabase executeUpdate:@"CREATE TABLE IF NOT EXISTS goodData (id text NOT NULL);"];
        if (result) {
            NSLog(@"创表成功");
        } else {
            NSLog(@"创表失败");
        }
    }
}
// 查询数据
- (void)queryData {
    int good = 0, collect = 0;
    if ([self.collectionDatabase open]) {
        // 1.执行查询语句
        FMResultSet *collectionResultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];
        // 2.遍历结果
        while ([collectionResultSet next]) {
            NSString *id = [collectionResultSet stringForColumn:@"id"];
            int idInt = [id intValue];
            int nowInt = [_nowID intValue];
            //NSLog(@"`````````%@ %@ %@",id,[collectionResultSet stringForColumn:@"mainLabel"],[collectionResultSet stringForColumn:@"imageURL"]);
            if (nowInt == idInt) {
                self.wkWebView.collectButton.tag = 104;
                [self.wkWebView.collectButton setImage:[UIImage imageNamed:@"shoucang3.png"] forState:UIControlStateNormal];
                collect = 1;
            }
        }
        [self.collectionDatabase close];
    }    
    if ([self.goodDatabase open]) {
        // 1.执行查询语句
        FMResultSet *goodResultSet = [self.goodDatabase executeQuery:@"SELECT * FROM goodData"];
        // 2.遍历结果
        while ([goodResultSet next]) {
            NSString *nowId = [NSString stringWithFormat:@"%@", [goodResultSet objectForColumn:@"id"]];
            int idInt = [nowId intValue];
            int nowInt = [_nowID intValue];
            if (idInt == nowInt) {
                self.wkWebView.goodButton.tag = 103;
                [self.wkWebView.goodButton setImage:[UIImage imageNamed:@"dianzan2.png"] forState:UIControlStateNormal];
                int goodNum = [self.wkWebView.goodLabel.text intValue];
                goodNum++;
                self.wkWebView.goodLabel.text = [NSString stringWithFormat:@"%d", goodNum];
                //break;
                good = 1;
            }
        }
        [self.goodDatabase close];
    }
    if (good == 0) {
        self.wkWebView.goodButton.tag = 3;
        [self.wkWebView.goodButton setImage:[UIImage imageNamed:@"dianzan.png"] forState:UIControlStateNormal];
        int goodNum = [self.wkWebView.goodLabel.text intValue];
        self.wkWebView.goodLabel.text = [NSString stringWithFormat:@"%d", goodNum];
    }
    if (collect == 0) {
        self.wkWebView.collectButton.tag = 4;
        [self.wkWebView.collectButton setImage:[UIImage imageNamed:@"shoucang.png"] forState:UIControlStateNormal];
    }
}

//插入数据
- (void)insertGoodData:(NSString *)string {
    if ([self.goodDatabase open]) {
        NSLog(@"%@",string);
        BOOL result = [self.goodDatabase executeUpdate:@"INSERT INTO goodData (id) VALUES (?);", string];
        if (!result) {
            NSLog(@"增加数据失败");
        } else {
            NSLog(@"增加数据成功");
        }
        [self.goodDatabase close];
    }
}

//插入数据
- (void)insertCollectionData:(NSString *)mainLabel and:(NSString *)imageURL and:(NSString *)nowId{
    if ([self.collectionDatabase open]) {
       
        BOOL result = [self.collectionDatabase executeUpdate:@"INSERT INTO collectionData (mainLabel, imageURL, id) VALUES (?, ?, ?);", mainLabel, imageURL, nowId];
        if (!result) {
            NSLog(@"增加数据失败");
        } else {
            NSLog(@"增加数据成功");
        }
        [self.collectionDatabase close];
    }
}
// 删除数据
- (void)deleteGoodData:(NSString *)nowId {
    if ([self.goodDatabase open]) {
        NSString *sql = @"delete from goodData WHERE id = ?";
        BOOL result = [self.goodDatabase executeUpdate:sql, nowId];
        if (!result) {
            NSLog(@"数据删除失败");
        } else {
            NSLog(@"数据删除成功");
        }
        [self.goodDatabase close];
    }
}

// 删除数据
- (void)deleteCollectionData:(NSString *)nowId {
    if ([self.collectionDatabase open]) {
        NSString *sql = @"delete from collectionData WHERE id = ?";
        BOOL result = [self.collectionDatabase executeUpdate:sql, nowId];
        if (!result) {
            NSLog(@"数据删除失败");
        } else {
            NSLog(@"数据删除成功");
        }
        [self.collectionDatabase close];
    }
}

关于离线加载

离线加载的主要应用其实也是关于FMDB,我的操作是在有网络的情况下每一次启动应用首先清空数据库,然后将此时首页请求到的数据存储在数据库里,待到下一次没有网络的时候app启动,将存储在数据库里的东西赋值上去,但是要注意一些原本该有的赋值以及点击函数的数组或字典为空,将这些操作终止掉,保证点击任何地方不会报错即可,我的方式是运用一个int类型的值,在请求成功和请求失败的情况下分别是不同的值,以此来判断是否应该触发点击事件以及刷新等函数。

- (void)firstNetworkRequest {
    [[MainManager sharedManage] NetWorkWithData:^(MainModel * _Nonnull mainViewModel) {
        NSLog(@"请求成功");
        self->_firstDictionary = [[NSMutableDictionary alloc] init];
        self->_firstDictionary = [mainViewModel toDictionary];
        NSLog(@"%@",self->_firstDictionary);
        [self->_allArray addObject:[mainViewModel toDictionary]];
        // 异步执行任务创建方法
        dispatch_async(dispatch_get_main_queue(), ^{            
            [self beforeNetworkRequest];
            [self deleteCacheData];
            for (int i = 0; i < 5; i++) {
                [self insertCacheData:self->_firstDictionary[@"top_stories"][i][@"title"] and:self->_firstDictionary[@"top_stories"][i][@"image"] and:self->_firstDictionary[@"top_stories"][i][@"hint"]];
            }            
            for (int i = 0; i < 6; i++) {
                [self insertCacheData:self->_firstDictionary[@"stories"][i][@"title"] and:self->_firstDictionary[@"stories"][i][@"images"][0] and:self->_firstDictionary[@"stories"][i][@"hint"]];
            }
        });
        
        
    } error:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //[self.mainView addNavigationControllerView];

            [self queryData];
            self.mainView.firstDictionary = self->_firstDictionary;
            NSLog(@"------%@",self.mainView.firstDictionary);
            self.mainView.section = 2;
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"通知" message:@"网络出现问题" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:sure];
            [self presentViewController:alertController animated:YES completion:nil];
            [self.mainView viewInit];
        });
        
        NSLog(@"请求失败");
    }];
}
//cell点进去
- (void)getClickNumber:(int)clickNumber {
    if (self.mainView.section == 4) {  //判断是否有网
        _viewController = [[WKWebViewController alloc] init];
        _viewController.delegate = self;
        if (_allArray[0][@"top_stories"] == nil) {
            NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
            dictionary = _allArray[0];
            _allArray[0] = _allArray[1];
            _allArray[1] = dictionary;
        }
        _viewController.clickNumber = clickNumber;
        _viewController.allArray = _allArray;
        _viewController.beforeDay = _beforeDay;
        _viewController.pushViewController = 0;
        [self.navigationController pushViewController:_viewController animated:YES];
    }
    
}

图片虚化效果

参考学长博客 iOS——简单实现图片渐变

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值