【iOS】——知乎日报第五周总结

文章目录

  • 一、评论区展开与收缩
  • 二、FMDB库实现本地持久化
      • FMDB常用类:
      • FMDB的简单使用:
  • 三、点赞和收藏的持久化


一、评论区展开与收缩

有的评论没有被回复评论或者被回复评论过短,这时就不需要展开全文的按钮,所以首先计算被回复评论的文本高度,根据文本高度来决定是否隐藏展开全文的按钮。

CGSize constrainedSize = CGSizeMake(WIDTH - 70, CGFLOAT_MAX); // labelWidth为UILabel的宽度,高度设置为无限大NSDictionary *attributes = @{NSFontAttributeName: cell.replyLabel.font}; // label为要计算高度的UILabel控件CGRect textRect = [cell.replyLabel.text boundingRectWithSize:constrainedSizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:attributescontext:nil];CGFloat textHeight = CGRectGetHeight(textRect);if (textHeight > 100) {cell.foldButton.hidden = NO;} else {cell.foldButton.hidden = YES;}

要实现评论区的展开全文和收起,需要先实现评论区文本的自适应高度,接着我用数组来记录每个按钮的状态,如果为展开状态则为1,如果为收起状态则为0。通过数组中按钮的状态为按钮的selected属性做出选择并改变cell的高度。当我点击按钮时就改变该按钮在数组中的相应值。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {if (indexPath.section == 0 && self.longComments != 0) {NSString* longStr = [self.discussModel.longCommentsArray[indexPath.row] content];NSString* longReplyStr = [self.discussModel.longCommentsArray[indexPath.row] reply_to][@"content"];CGSize constrainedSize = CGSizeMake(WIDTH - 70, CGFLOAT_MAX); // labelWidth为UILabel的宽度,高度设置为无限大UIFont *font = [UIFont systemFontOfSize:18.0];NSDictionary *attributes = @{NSFontAttributeName: font}; // label为要计算高度的UILabel控件CGRect textRect = [longStr boundingRectWithSize:constrainedSizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:attributescontext:nil];CGFloat textHeight = CGRectGetHeight(textRect);CGSize constrainedSize02 = CGSizeMake(WIDTH - 70, CGFLOAT_MAX); // labelWidth为UILabel的宽度,高度设置为无限大UIFont *font02 = [UIFont systemFontOfSize:16.0];NSDictionary *attributes02 = @{NSFontAttributeName: font02}; // label为要计算高度的UILabel控件CGRect textRect02 = [longReplyStr boundingRectWithSize:constrainedSize02options:NSStringDrawingUsesLineFragmentOriginattributes:attributes02context:nil];CGFloat textHeight02 = CGRectGetHeight(textRect02);NSLog(@"长评论高度为:%f", textHeight);if ([self.discussModel.longButtonSelectArray[indexPath.row] isEqualToString:@"1"]) {return textHeight + textHeight02 + 180;}return textHeight + 180;} else {NSString* shortStr = [self.discussModel.shortCommentsArray[indexPath.row] content];NSString* shortReplyStr = [self.discussModel.shortCommentsArray[indexPath.row] reply_to][@"content"];CGSize constrainedSize = CGSizeMake(WIDTH - 70, CGFLOAT_MAX); // labelWidth为UILabel的宽度,高度设置为无限大UIFont *font = [UIFont systemFontOfSize:18.0];NSDictionary *attributes = @{NSFontAttributeName: font}; // label为要计算高度的UILabel控件CGRect textRect = [shortStr boundingRectWithSize:constrainedSizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:attributescontext:nil];CGFloat textHeight = CGRectGetHeight(textRect);CGSize constrainedSize02 = CGSizeMake(WIDTH - 70, CGFLOAT_MAX); // labelWidth为UILabel的宽度,高度设置为无限大UIFont *font02 = [UIFont systemFontOfSize:16.0];NSDictionary *attributes02 = @{NSFontAttributeName: font02}; // label为要计算高度的UILabel控件CGRect textRect02 = [shortReplyStr boundingRectWithSize:constrainedSize02options:NSStringDrawingUsesLineFragmentOriginattributes:attributes02context:nil];CGFloat textHeight02 = CGRectGetHeight(textRect02);NSLog(@"段评论高度为:%f", textHeight);if ([self.discussModel.shortButtonSelectArray[indexPath.row] isEqualToString:@"1"]) {return textHeight + textHeight02 + 180;}return textHeight + 180;}  
}- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0 && self.longComments != 0) {if (indexPath.row == 0) {cell.commentsNumLabel.text = [NSString stringWithFormat:@"%ld条长评", self.longComments];}cell.foldButton.tag = indexPath.row * 2 + 1;if ([self.discussModel.longButtonSelectArray[indexPath.row] isEqualToString:@"1"]) {cell.foldButton.selected = YES;cell.replyLabel.numberOfLines = 0;} else {cell.foldButton.selected = NO;cell.replyLabel.numberOfLines = 2;}
}else {cell.foldButton.tag = indexPath.row * 2;if ([self.discussModel.shortButtonSelectArray[indexPath.row] isEqualToString:@"1"]) {cell.foldButton.selected = YES;cell.replyLabel.numberOfLines = 0;} else {cell.foldButton.selected = NO;cell.replyLabel.numberOfLines = 2;}
- (void)pressFold:(UIButton*)button {DiscussCustomCell* cell = (DiscussCustomCell*)[[button superview] superview];if ([self.discussModel.longButtonSelectArray[(button.tag - 1) / 2] isEqualToString:@"1"]) {[self.discussModel.longButtonSelectArray replaceObjectAtIndex:((button.tag - 1) / 2) withObject:@"0"];[self.discussView.tableview reloadData];} else {[self.discussModel.longButtonSelectArray replaceObjectAtIndex:((button.tag - 1) / 2) withObject:@"1"];[self.discussView.tableview reloadData];}if ([self.discussModel.shortButtonSelectArray[button.tag / 2] isEqualToString:@"1"]) {[self.discussModel.shortButtonSelectArray replaceObjectAtIndex:(button.tag / 2) withObject:@"0"];[self.discussView.tableview reloadData];} else {[self.discussModel.shortButtonSelectArray replaceObjectAtIndex:(button.tag / 2) withObject:@"1"];[self.discussView.tableview reloadData];}
}

请添加图片描述

请添加图片描述

二、FMDB库实现本地持久化

FMDB是iOS平台的SQLite数据库框架,以OC的方式封装了SQLite的C语言API。

FMDB常用类:

FMDatabase:一个FMDatabase对象就代表一个单独的SQLite数据库用来执行SQL语句。
FMResultSet:使用FMDatabase执行查询后的结果集。
FMDatabaseQueue:用于在多线程中执行多个查询或更新,它是线程安全的。

FMDB的简单使用:

要使用FMDB库首先需要通过CocoaPods来安装这个第三方库

pod 'FMDB'

安装完成之后就可以进行使用了。

创建数据库

//获取数据库文件的路径NSString* doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSString *fileName = [doc stringByAppendingPathComponent:@"collectionData.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, nameLabel text NOT NULL, imageURL text NOT NULL, networkURL text NOT NULL, dateLabel text NOT NULL, nowLocation text NOT NULL, goodState text NOT NULL, collectionState text NOT NULL, id text NOT NULL);"];if (result) {NSLog(@"创表成功");} else {NSLog(@"创表失败");}}

数据库增加数据

//插入数据
- (void)insertData {if ([self.collectionDatabase open]) {NSString *string = @"GenShen";BOOL result = [self.collectionDatabase executeUpdate:@"INSERT INTO collectionData (mainLabel, nameLabel, imageURL, networkURL, dateLabel, nowLocation, goodState, collectionState, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", string, string, string, string, string, string, string, string, string];if (!result) {NSLog(@"增加数据失败");}else{NSLog(@"增加数据成功");}[self.collectionDatabase close];}
}

数据库修改数据

//修改数据
- (void)updateData {if ([self.collectionDatabase open]) {NSString* sql = @"UPDATE collectionData SET id = ? WHERE nameLabel = ?";BOOL result = [self.collectionDatabase executeUpdate:sql, @"114514",@"GenShen"];if (!result) {NSLog(@"数据修改失败");} else {NSLog(@"数据修改成功");}[self.collectionDatabase close];}
}

数据库删除数据

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

数据库查询数据

//查询数据
- (void)queryData {if ([self.collectionDatabase open]) {FMResultSet* resultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];while ([resultSet next]) {NSString *mainLabel = [resultSet stringForColumn:@"mainLabel"];NSLog(@"mainLabel = %@",mainLabel);NSString *nameLabel = [resultSet stringForColumn:@"nameLabel"];NSLog(@"nameLabel = %@",nameLabel);NSString *imageURL = [resultSet stringForColumn:@"imageURL"];NSLog(@"imageURL = %@",imageURL);NSString *networkURL = [resultSet stringForColumn:@"networkURL"];NSLog(@"networkURL = %@",networkURL);NSString *dateLabel = [resultSet stringForColumn:@"dateLabel"];NSLog(@"dateLabel = %@",dateLabel);NSString *nowLocation = [resultSet stringForColumn:@"nowLocation"];NSLog(@"nowLocation = %@",nowLocation);NSString *goodState = [resultSet stringForColumn:@"goodState"];NSLog(@"goodState = %@",goodState);NSString *collectionState = [resultSet stringForColumn:@"collectionState"];NSLog(@"collectionState = %@",collectionState);NSString *id = [resultSet stringForColumn:@"id"];NSLog(@"id = %@",id);}[self.collectionDatabase close];}
}

三、点赞和收藏的持久化

要实现点赞和收藏的持久化就需要用到前面所提到的FMDB库进行操作。在需要用到数据库的部分进行数据库的创建和一些基本操作例如增删改查。以收藏持久化为例子,当点击点赞收藏按钮的时候,进入到数据库查询函数进行查询,如果找到就将该数据进行删除然后将按钮状态设置为未选中状态。如果没有找到该数据就进行添加然后将按钮状态设置为选中状态

- (void)pressCollect:(UIButton*)button {NSString* collectionIdStr = [NSString stringWithFormat:@"%ld", self.idStr];int flag = [self queryCollectionData];if (flag == 1) {self.mainWebView.collectButton.selected = NO;[self deleteCollectionData:collectionIdStr];} else {self.mainWebView.collectButton.selected = YES;[self insertCollectionData:self.mainLabel andUrl:self.imageUrl andStr:collectionIdStr];}
}

当滑动切换页面请求新闻额外信息时也需要先进行判断当前点赞和收藏的按钮状态

[[ExtraManager sharedSingleton] ExtraGetWithData:^(GetExtraModel * _Nullable extraModel) {dispatch_async(dispatch_get_main_queue(), ^{int flag = [self queryLikesData];int collectFlag = [self queryCollectionData];self.mainWebView.discussLabel.text = [NSString stringWithFormat:@"%ld", extraModel.comments];self.mainWebView.likeLabel.text = [NSString stringWithFormat:@"%ld", extraModel.popularity];self.longComments = extraModel.long_comments;self.shortComments = extraModel.short_comments;if (flag == 1) {self.mainWebView.likeButton.selected = YES;NSInteger likesNum = [self.mainWebView.likeLabel.text integerValue];likesNum++;self.mainWebView.likeLabel.text = [NSString stringWithFormat:@"%ld", likesNum];NSString* likesIdStr = [NSString stringWithFormat:@"%ld", self.idStr];[self insertLikesData:likesIdStr];} else {self.mainWebView.likeButton.selected = NO;}if (collectFlag == 1) {self.mainWebView.collectButton.selected = YES;} else {self.mainWebView.collectButton.selected = NO;}NSLog(@"额外信息获取成功");});} andError:^(NSError * _Nullable error) {NSLog(@"额外信息获取失败");} andIdStr:self.idStr];

收藏数据库部分代码如下:

//创建
- (void)databaseInit {NSString* likesDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSLog(@"%@", likesDoc);NSString * likesFileName = [likesDoc stringByAppendingPathComponent:@"likesData.sqlite"];self.likesDatabase = [FMDatabase databaseWithPath:likesFileName];if ([self.likesDatabase open]) {BOOL result = [self.likesDatabase executeUpdate:@"CREATE TABLE IF NOT EXISTS likesData (id text NOT NULL);"];if (result) {NSLog(@"创表成功");} else {NSLog(@"创表失败");}}NSString* collectionDoc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSLog(@"%@", collectionDoc);NSString * collectionFileName = [collectionDoc stringByAppendingPathComponent:@"collectionData02.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(@"创表失败");}}}
//增加
- (void)insertCollectionData:(NSString *)mainLabel andUrl:(NSString *)imageURL andStr:(NSString*)string {if ([self.collectionDatabase open]) {BOOL result = [self.collectionDatabase executeUpdate:@"INSERT INTO collectionData (mainLabel, imageURL, id) VALUES (?, ?, ?);", mainLabel, imageURL,string];if (!result) {NSLog(@"增加收藏数据失败");}else{NSLog(@"增加收藏数据成功");}}[self.collectionDatabase close];
}
//删除
- (void)deleteCollectionData:(NSString*)string {if ([self.collectionDatabase open]) {NSString *sql = @"delete from collectionData WHERE id = ?";BOOL result = [self.collectionDatabase executeUpdate:sql, string];if (!result) {NSLog(@"删除收藏数据失败");}else{NSLog(@"删除收藏数据成功");}}[self.collectionDatabase close];
}//查询
- (int)queryCollectionData {if ([self.collectionDatabase open]) {FMResultSet* collectionResultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];int count = 0;while ([collectionResultSet next]) {NSString *sqlIdStr = [NSString stringWithFormat:@"%@", [collectionResultSet objectForColumn:@"id"]];NSInteger sqlId = [sqlIdStr integerValue];NSLog(@"第 %d 个收藏数据库数据:%ld", count++ ,sqlId);if (self.idStr == sqlId) {[self.collectionDatabase close];return 1;}}}[self.collectionDatabase close];return 0;
}

请添加图片描述

请添加图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/195609.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汽车标定技术--A2L格式分析

目录 1.A2L由来 2.A2L格式 2.1 PROJECT 2.2 MODULE中包含的内容 3. INCA和CANape兼容吗? 最近有朋友用Vector ASAP2Editor编译的A2L文件在INCA7.4中无法识别,我记得以前做的时候是可以识别的,难不成最近有什么变动吗?出于好…

【Gradle-13】SNAPSHOT版本检查

1、什么是SNAPSHOT SNAPSHOT版本是指尚未发布的版本,是一个「动态版本」,它始终指向最新的发布工件(gav),也就是说同一个SNAPSHOT版本可以反复用来发布。 这种情况在大型app多团队的开发中比较常见,比如us…

window上Clion配置C++版本的opencv

window上Clion配置opencv 注意版本一定要对的上,否则可能会出错,亲测 widnows 11mingw 8.1.0opencv 4.5.5 mingw8.1下载地址https://sourceforge.net/projects/mingw/ 配置环境变量 cmake下载 安装完添加环境变量 来到官网,下载 windows 对…

基于java web个人财务管理系统

末尾获取源码 开发语言:Java Java开发工具:JDK1.8 后端框架:SSM 前端:采用JSP技术开发 数据库:MySQL5.7和Navicat管理工具结合 服务器:Tomcat8.5 开发软件:IDEA / Eclipse 是否Maven项目&#x…

概念解析 | 网络安全数字孪生(Digital Twin of Cyber Security, DTCS)技术

注1:本文系“概念解析”系列之一,致力于简洁清晰地解释、辨析复杂而专业的概念。本次辨析的概念是:网络安全数字孪生。 概念解析 | 网络安全的“数字镜像” —— 网络安全数字孪生 1. 背景介绍 随着数字化转型进程的深入推进,网络空间安全问题日益凸显。当前的网络安全防护面…

影刀sqlite的插入方法

影刀sqlite的插入方法 变量外面不用加‘’

常见树种(贵州省):002杉类

摘要:本专栏树种介绍图片来源于PPBC中国植物图像库(下附网址),本文整理仅做交流学习使用,同时便于查找,如有侵权请联系删除。 图片网址:PPBC中国植物图像库——最大的植物分类图片库 一、杉木 …

「项目阅读系列」go-gin-example star 6.5k!(1)

文章目录 准备工作适宜人群项目信息 项目结构代码阅读主要模块代码主函数模块router 路由模块auth 授权模块数据库 修改文章请求分析其他依赖 总结 准备工作 适宜人群 初学 go 语法,希望了解 go 项目的构建过程和方式。 项目信息 go-gin-example 项目是使用 gin…

integrin + Receptor ; platelet ; Ig-CAM

platelet HPA ; integrin ; Cell adhesion in cancer: Beyond the migration of single cells

ROS参数服务器(Param):通信模型、Hello World与拓展

参数服务器在ROS中主要用于实现不同节点之间的数据共享。 参数服务器相当于是独立于所有节点的一个公共容器,可以将数据存储在该容器中,被不同的节点调用,当然不同的节点也可以往其中存储数据。 使用场景一般存储一些机器人的固有参数&…

大反转!OpenAI董事会辞职,求奥特曼重返OpenAI?「奥特曼24小时流放」大揭秘...

大家好,我是二狗。 想必大家昨天都被Sam Altman被董事会解雇的事情刷屏了。 然而才仅仅过去一天,OpenAI 董事会就反悔了!正和Sam Altman 商量让他重返CEO职位。 这一反转和Altman被炒鱿鱼一样突然,凄凄惨惨真真假假真真&#x…

通过汇编理解cortex-m3:第0章

第0章:准备工作 基本想法:利用汇编和gdb调试,来学习cortex-m3汇编指令,以及一些寄存器的功能。 软件和硬件: 硬件:韦东山瑞士军刀中的最小核心板(STM32F103C8T6) STLINK-V2&#…