当前位置: 首页 > news >正文

仿写知乎日报第三周

新学到的

本周新学习了FMDB数据库,并对Masonry的使用有了更近一步的了解,还了解了cell的自适应高度

  • FMDB数据库的介绍和使用:iOS——FMDB的介绍与使用

cell自适应高度和Mansonry自动布局

本周写了评论区,在写评论区的时候,学到了cell的自适应高度。首先是要将tableView的rowHeight属性设置为UITableViewAutomaticDimension,这样就可以使cell自动适应cell内容的高度。如下:

        self.pingLunView.tableView.rowHeight = UITableViewAutomaticDimension;

然后要对cell中的控件位置使用Masonry自动布局。因为这个cell自适应高度是要依靠cell中的内容的布局的,相当于靠cell中内容的大小将cell撑大。因此在cell中的Masonry布局中,我们只要设置控件的left、right、top、bottom即可,要注意这块一定要把布局设置合理了,我在写布局的时候两个控件的布局发生冲突,程序虽然没崩但是一直报警告。代码实现如下:

    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView).offset(70);make.top.equalTo(self.contentView).offset(10);make.width.equalTo(@(self.contentView.bounds.size.width - 90));make.height.equalTo(@30);}];[self.pingLunLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.nameLabel.mas_left);make.top.equalTo(self.nameLabel.mas_bottom).offset(10);make.right.equalTo(self.contentView.mas_right).offset(-20);make.bottom.equalTo(self.replylabel.mas_top).offset(-10);}];[self.dianZanButton mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.contentView.mas_right).offset(-60);make.top.equalTo(self.contentView.mas_bottom).offset(-30);make.right.equalTo(self.contentView.mas_right).offset(-30);make.bottom.equalTo(self.contentView.mas_bottom).offset(-30);}];[self.replylabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.pingLunLabel.mas_left);make.top.equalTo(self.pingLunLabel.mas_bottom).offset(10);make.right.equalTo(self.pingLunLabel.mas_right);make.bottom.equalTo(self.contentView.mas_bottom).offset(-50);}];

使用FMDB本地存储数据

在实现本地持久化时,我用到了FMDB。首先我在收藏的功能中使用了FMDB,具体用法是在收藏的Model层中写了FMDB的相关方法,在显示具体页面的部分有收藏按钮,如果点击了收藏按钮,就将当前页面的id、url、title、hint、image属性以及收藏和点赞的BOOL值使用FMDB的增加方法加入数据库,反之如果取消收藏按钮,就将该数据删除。然后在收藏的tableView中将FMDB中的数据放入cell中,就可以完成收藏的本地持久化。
在从数据库中查找数据的方法,我返回的是一个数组,这个数组中存放着字典。

- (NSArray*)findDataWithChoice: (BOOL) isurl{if ([self.collectionDatabase open]) {NSMutableArray *array = [[NSMutableArray alloc] init];if (isurl == YES) {FMResultSet *resultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];while ([resultSet next]) {NSString *url = [resultSet stringForColumn:@"url"];NSString *idstr = [resultSet stringForColumn:@"id"];NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: idstr, @"ID", url, @"url", nil];[array addObject:dict];}[self.collectionDatabase close];return array;} else {FMResultSet *resultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];while ([resultSet next]) {NSString *titleStr = [resultSet stringForColumn:@"title"];NSString *hintStr = [resultSet stringForColumn:@"hint"];NSString *imageStr = [resultSet stringForColumn:@"image"];NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:titleStr, @"title", hintStr, @"hint", imageStr, @"image", nil];[array addObject:dict];}[self.collectionDatabase close];return array;}}return nil;
}

对之前写的内容的优化修改

在一开始写评论区的时候,头像的获取的图片的请求我是放在cell中的,导致每当出现cell出现和消失的时候都会再次请求一遍头像,因此我修改为将请求的头像放在一个数组中再赋值,就可以让cell只请求一遍,不会反复请求。

效果图

评论区:
在这里插入图片描述
在这里插入图片描述
收藏:
在这里插入图片描述

还有问题的地方

评论区的折叠展开评论功能,因为获取label的高度还有问题,因此还没有完成
收藏功能因为收藏顺序和加入数据库的顺序相反,因此在左右滑动收藏的页面时有bug
点赞和收藏的状态还没有写完

http://www.lryc.cn/news/219323.html

相关文章:

  • Godot Best practices
  • win10 + cmake3.17 编译 giflib5.2.1
  • 【rust/esp32】初识slint ui框架并在st7789 lcd上显示
  • 精通Nginx(05)-http工作机制、指令和内置变量
  • 用于 GaN-HEMT 功率器件仿真的 TCAD 方法论
  • Web3公链之Cosmos生态的项目Celestia
  • vue+prismjs 网页代码高亮插件
  • 【软件测试】其实远远不止需求文档这么简单
  • SAP-PP-常用TCODE
  • 第六章认识Node.js服务器开发
  • Ubuntu 增加服务 比如openfire
  • 海康Visionmaster-全局变量:全局变量关联流程中具体 模块结果的方法
  • Eureka介绍和使用
  • Incremental Object Detection via Meta-Learning【论文解析】
  • AI大模型时代网络安全攻防对抗升级,瑞数信息变革“下一代应用与数据安全”
  • 后端接口接收对象和文件集合,formdata传递数组对象
  • python json包
  • 基于 NGram 分词,优化 Es 搜索逻辑,并深入理解了 matchPhraseQuery 与 termQuery
  • PivotNet:Vectorized Pivot Learning for End-to-end HD Map Construction
  • 阿里云安全恶意程序检测
  • Xcode中如何操作Git
  • 浅述边缘计算场景下的云边端协同融合架构的应用场景示例
  • C++中禁止在栈中实例化的类
  • MsgPack和Protobuf
  • 自定义类型联合体
  • 【Shell 系列教程】Shell printf 命令( 六)
  • 2022年电工杯数学建模B题5G网络环境下应急物资配送问题求解全过程论文及程序
  • git reflog 恢复git reset --hard 回退的内容
  • kali Linux中更换为阿里镜像源
  • 【每日一题】移除链表元素(C语言)