知乎日报第三周博客

一. cell高度自适应

  • 一开始我尝试自行计算cell高度,但是这种方法太繁琐,所以之后换了一种方法。
  • 我尝试使用了masonry约束撑开cell高度,但一直失败,撑不开cell。
  • 在调试之后发现,之所以撑不开cell是因为我把约束写在layoutSubviews方法中。而需要返回cell高度时,这个方法还没有运行,所以无法撑开cell。
  • 再将约束改到initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法中后成功撑开cell。

自适应方法:

  1. 初始化tableView,设置rowHeight = UITableViewAutomaticDimension并设置预设高度(随便设)。
self.tableView.estimatedRowHeight = 300;
self.tableView.rowHeight = UITableViewAutomaticDimension;
  1. 在cell的- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法内画好约束。
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.userIcon = [[UIImageView alloc] init];
        self.userIcon.layer.masksToBounds = YES;
        self.userIcon.layer.cornerRadius = USERICON_SIZE / 2;
        
        self.userName = [[UILabel alloc] init];
        [self.userName setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        
        self.userComment = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.userComment setFont:[UIFont systemFontOfSize:18]];
        self.userComment.numberOfLines = 0;
        
        self.time = [[UILabel alloc] init];
        [self.time setFont:[UIFont systemFontOfSize:14]];
        self.time.textColor = [UIColor grayColor];
        
        self.timeDay = [[UILabel alloc] init];
        [self.timeDay setFont:[UIFont systemFontOfSize:14]];
        self.timeDay.textColor = [UIColor grayColor];
        
        self.likeNumber = [[UILabel alloc] init];
        [self.likeNumber setFont:[UIFont systemFontOfSize:14]];
        self.likeNumber.textColor = [UIColor grayColor];
        
        self.likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.likeButton.tintColor = [UIColor grayColor];
        
        self.commentButton = [[UIImageView alloc] init];
        self.commentButton.tintColor = [UIColor grayColor];
        
        [self.contentView addSubview:self.userIcon];
        [self.contentView addSubview:self.userName];
        [self.contentView addSubview:self.userComment];
        [self.contentView addSubview:self.time];
        [self.contentView addSubview:self.timeDay];
        [self.contentView addSubview:self.likeNumber];
        [self.contentView addSubview:self.likeButton];
        [self.contentView addSubview:self.commentButton];
        
        [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(20);
            make.top.equalTo(self.contentView.mas_top).offset(20);
            make.size.mas_equalTo(USERICON_SIZE);
        }];
        
        [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(10);
            make.top.equalTo(self.contentView.mas_top).offset(22);
            make.width.mas_offset(SCREEN_SIZE_WIDTH);
        }];
        
        [self.userComment mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).mas_offset(48);
            make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(10);
            make.right.mas_equalTo(-20);
            make.height.mas_lessThanOrEqualTo(SCREEN_SIZE_HEIGHT);
            make.bottom.equalTo(self.contentView).mas_offset(-60);
        }];
        
        [self.timeDay mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(10);
            make.bottom.mas_equalTo(self.contentView).mas_offset(-20);
        }];
        
        [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentView).mas_offset(-20);
            make.left.mas_equalTo(self.timeDay.mas_right).mas_offset(10);
        }];
        
        [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentView).mas_offset(-20);
            make.right.mas_equalTo(-20);
            make.size.mas_equalTo(20);
        }];
        
        [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make ){
            make.bottom.mas_equalTo(self.contentView).mas_offset(-20);
            make.right.mas_equalTo(self.commentButton.mas_left).mas_offset(-30);
            make.size.mas_equalTo(20);
        }];
        
        [self.likeNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentView).mas_offset(-20);
            make.right.mas_equalTo(self.likeButton.mas_left);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(20);
        }];
    }
    return self;
}
  1. 完成tableView的其他协议方法。
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.commentDict count];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 && self.commentDict[@"long"] != nil) {
        return [self.commentDict[@"long"] count];
    } else {
        return [self.commentDict[@"short"] count];
    }
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* cellName = @"commentReplyedCell";
            
            CommentReplyedCell* cell = [self.tableView dequeueReusableCellWithIdentifier:cellName];
            
            if (cell == nil) {
                cell = [[CommentReplyedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
            }
            
            ···
            
            return cell;
}

二. 使用dispatch_group控制网络请求顺序。

dispatch_group_t group = dispatch_group_create();
    
    if (self.isLong) {
        dispatch_group_enter(group);
        [[Manage sharedManage] getLongCommentWithID:self.ID CommentData:^(CommentModel * _Nonnull commentModel) {
            NSDictionary* dict = [commentModel toDictionary];
            [self.commentDict setObject:dict[@"comments"] forKey:@"long"];
            self.isLong = YES;
            dispatch_group_leave(group);
        } error:^(NSError * _Nonnull error) {
            NSLog(@"get longComment error");
            dispatch_group_leave(group);
        }];
    }
    
    if (self.isShort) {
        dispatch_group_enter(group);
        [[Manage sharedManage] getShortCommentWithID:self.ID CommentData:^(CommentModel * _Nonnull commentModel) {
            NSDictionary* dict = [commentModel toDictionary];
            [self.commentDict setObject:dict[@"comments"] forKey:@"short"];
            self.isShort = YES;
            dispatch_group_leave(group);
        } error:^(NSError * _Nonnull error) {
            NSLog(@"get shortComment error");
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            self.commentView.commentDict = self.commentDict;
            [self.commentView viewReload];
        });
    });

简单来说就是在网络请求前enter,在网络请求成功后leave,当enter等于leave量时,dispatch_group_notify才会执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值