objectivec-tableView单元格上的iOS砌石



我在表视图单元格上使用Masonry,现在我有一个UITableViewCell,它是视图的容器,类似于以下内容:

*表视图(cellForRowAtIndexPath):

MasonryTestTableViewCell *masonryCell = [tableView dequeueReusableCellWithIdentifier:@"masonryCell"];
if (masonryCell == nil) {
    masonryCell = [[MasonryTestTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"masonryCell"];
}
[masonryCell addSubview:[self createViewForCell]];
return masonryCell;

*createViewForCell方法(也使用砖石结构):

-(UIView *) createViewForCell{
    self.textLabel = [[UILabel alloc]init];
    [self.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.textLabel setTextAlignment:NSTextAlignmentLeft];
    [self.textLabel setNumberOfLines:0];
    [self.textLabel setText:@"TEST TEXT"];
    [self.textLabel setPreferredMaxLayoutWidth:[UIScreen mainScreen].bounds.size.width];
    [self addSubview:self.textLabel];
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(10);
        make.bottom.equalTo(self.mas_bottom).with.offset(-10);
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.top.equalTo(self.mas_top).with.offset(10);
    }];
    self.textLabel.layer.borderColor = [UIColor grayColor].CGColor;
    self.textLabel.layer.borderWidth = 3;
}

*@实现MasonryTestTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    return [super initWithStyle:style reuseIdentifier:reuseIdentifier];
}
- (void) setContent:(UIView *)view{
    [self setBackgroundColor:[UIColor greenColor]];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).with.offset(kYPosition);
            make.bottom.equalTo(self.mas_bottom).with.offset(-kYPosition);
            make.left.equalTo(self.mas_left).with.offset(kCellPadding);
            make.right.equalTo(self.mas_right).with.offset(-kCellPadding);
     }];
}

我现在面临的问题是,这个单元格没有正确地展开,如果我在textLabel上设置一个长文本,它不会增加单元格的高度,我无法解决这个问题,你知道人工智能是否应该做其他事情来让它发挥作用吗?

Masonry只是NSAutolayoutConstraint功能的包装框架。为了正确调整单元格大小,您应该通过向表格提供高度信息查看

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return [self heightForBasicCellAtIndexPath:indexPath];
}

方法heightForBasicCellAtIndexPath:可能看起来像

- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath {
  static UITableViewCell *sizingCell = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sizingCell = [self.tableView dequeueReusableCellWithIdentifier:@"your_Cell_identifier"];
  });
  [self configureBasicCell:sizingCell atIndexPath:indexPath];
  return [self calculateHeightForConfiguredSizingCell:sizingCell];
}
- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {
  [sizingCell setNeedsLayout];
  [sizingCell layoutIfNeeded];
  CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
  return size.height + 1.0f; // Add 1.0f for the cell separator height
}

最后一件事是定义configureBasicCell: atIndexPath:。在您的情况下,它可能看起来像:

- (void)configureBasicCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
 [cell addSubview:[self createViewForCell]];
}

有关更多信息,请参阅此

希望这能帮助

Doro展示了一种解决问题的方法,但我认为如何处理问题取决于您选择动态设置单元格高度的方式
如果使用autoLayout和UITableViewAutomaticDimension,则不必通过代码计算高度,只需使内部子视图拉伸单元格的高度即可
在这种情况下,我在你的代码中发现了一个错误,你必须让cell.contentView来添加Subview,而不是单元格本身
我希望它能起作用,祝你好运!

最新更新