仿写知乎日报第三周

编程入门 行业动态 更新时间:2024-10-28 12:29:50

仿写知乎日报<a href=https://www.elefans.com/category/jswz/34/1767204.html style=第三周"/>

仿写知乎日报第三周

新学到的

本周新学习了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
点赞和收藏的状态还没有写完

更多推荐

仿写知乎日报第三周

本文发布于:2023-11-16 12:33:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第三周   日报   仿写

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!