[iOS]--知乎日报第四周总结

[iOS]--知乎日报第四周总结

  • 一.评论区
    • 自适应行高
  • 二.本地持久化

一.评论区

自适应行高

可以通过Massonry来实现效果,可以借助 UITableView 的 estimatedRowHeight 和 rowHeight 属性,以及 Masonry 的自动布局功能。以下是实现步骤:
首先,确保你的 UITableViewCell 中的子视图使用了 Masonry 进行自动布局。

在 UITableView 的数据源方法中,设置 estimatedRowHeight 和 rowHeight 属性。estimatedRowHeight 用于估算行高,rowHeight 用于设置具体的行高。

tableView.estimatedRowHeight = 100; // 估算的行高
tableView.rowHeight = UITableViewAutomaticDimension; // 设置为自动计算高度

在 UITableViewCell 的 - (void)awakeFromNib 方法中,添加子视图并设置 Masonry 约束。

- (void)awakeFromNib {
    [super awakeFromNib];
    // 添加子视图并设置Masonry约束
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(8);
        make.left.equalTo(self.contentView.mas_left).offset(8);
        make.right.equalTo(self.contentView.mas_right).offset(-8);
    }];
    // 添加其他子视图并设置Masonry约束
}

通过上述步骤,你就可以使用 Masonry 实现自适应高度。当内容发生变化时,UITableView 会根据实际内容重新计算高度,从而实现动态高度的 UITableViewCell。

将masonry布局放在以下方法中,才能在调用时正确撑开cell。

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

想要撑开cell必须保证你的布局内包括对contentView的top与bottm都要有所约束。

[self.imagebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(30);
    }];
    
    [self.namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //由于要自适应的cell,所以不用宽高约束
        make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
        make.top.equalTo(self.imagebtn.mas_top).offset(-5);
        make.width.mas_offset(300);
    }];
 
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
        make.top.equalTo(self.namelabel.mas_bottom).offset(5);
        make.right.mas_equalTo(-20);
        make.width.mas_offset(300);
        make.height.mas_lessThanOrEqualTo(self.contentView.bounds.size.height);
    }];
 
     [self.sublabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
         make.top.equalTo(self.label.mas_bottom).offset(5);
         make.width.mas_offset(300);
     }];
    [self.timelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
        make.top.equalTo(self.sublabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.width.mas_offset(300);
    }];
    [self.commentbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sublabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(20);
    }];
    [self.likebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sublabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.right.mas_equalTo(self.commentbtn.mas_left).mas_offset(-30);
        make.size.mas_equalTo(20);
    }];
[self.imagebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(30);
    }];
    
    [self.namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //由于要自适应的cell,所以不用宽高约束
        make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
        make.top.equalTo(self.imagebtn.mas_top).offset(-5);
        make.width.mas_offset(300);
    }];
 
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
        make.top.equalTo(self.namelabel.mas_bottom).offset(5);
        make.right.mas_equalTo(-20);
        make.width.mas_offset(300);
        make.height.mas_lessThanOrEqualTo(self.contentView.bounds.size.height);
    }];
 
     [self.sublabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
         make.top.equalTo(self.label.mas_bottom).offset(5);
         make.width.mas_offset(300);
     }];
    [self.timelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imagebtn.mas_right).mas_offset(10);
        make.top.equalTo(self.sublabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.width.mas_offset(300);
    }];
    [self.commentbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sublabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(20);
    }];
    [self.likebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sublabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.right.mas_equalTo(self.commentbtn.mas_left).mas_offset(-30);
        make.size.mas_equalTo(20);
    }];

二.本地持久化

在写点赞收藏的时候要用到本地持久化,所以就先学了一下FMDB库,了解一下基本的方法。

FMDB有三个主要的类:

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

获取路径

NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", doc);
    NSString *fileName = [doc stringByAppendingPathComponent:@"collectionData.sqlite"];

获取打开

FMDatabase *collectionDatabase = [FMDatabase databaseWithPath:fileName];
 BOOL executeUpdate = [dataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS t_collect (id text NOT NULL);"];
                    if (executeUpdate) {
                        NSLog(@"创建表成功");
                    } else {
                        NSLog(@"创建表失败");
                    }

通常对数据库的操作,我们一般称为CURD,即对表进行创建(Create)、更新(Update)、读取(Read)和删除(Delete)操作。

插入数据

//插入数据
- (void)insertData {
    if ([self.collectionDatabase open]) {
        NSString *string = @"hi world";
        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, @"1", @"hi world"];
        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, @"xixixixi"];
        if (!result) {
            NSLog(@"数据删除失败");
        } else {
            NSLog(@"数据删除成功");
        }
        [self.collectionDatabase close];
    }
}

查找数据

// 查询数据
- (void)queryData {
    if ([self.collectionDatabase open]) {
        // 1.执行查询语句
        FMResultSet *resultSet = [self.collectionDatabase executeQuery:@"SELECT * FROM collectionData"];
        // 2.遍历结果
        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];
    }
}

你可能感兴趣的:(ios,cocoa,macos)