Resize UITableViewCell containing UITextView upon typing

后端 未结 11 2141
感动是毒
感动是毒 2020-12-23 10:19

I have an UITableViewController that contains a custom cell. Each cell was created using a nib and contains a single non-scrollable UITextView. I have added constraints insi

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 11:03

    I've implemented a similar approach using a UITextView however to do so I had to implement heightForRowAtIndexPath

    #pragma mark - SizingCell
    
    - (USNTextViewTableViewCell *)sizingCell
    {
        if (!_sizingCell)
        {
            _sizingCell = [[USNTextViewTableViewCell alloc] initWithFrame:CGRectMake(0.0f,
                                                                                     0.0f,
                                                                                     self.tableView.frame.size.width,
                                                                                     0.0f)];
        }
    
        return _sizingCell;
    }
    
    #pragma mark - UITableViewDelegate
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.sizingCell.textView.text = self.profileUpdate.bio;
    
        [self.sizingCell setNeedsUpdateConstraints];
        [self.sizingCell updateConstraintsIfNeeded];
    
        [self.sizingCell setNeedsLayout];
        [self.sizingCell layoutIfNeeded];
    
        CGSize cellSize = [self.sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    
        return cellSize.height;
    }
    

    sizingCell is an instance of the cell that is only used for sizing calculations.

    What's important to note is that you need to attach the UITextView's upper and lower edge to the UITableViewCells contentView's upper and lower edge so that as the UITableViewCell changes in height the UITextView also changes in height.

    For constraint layout I use a PureLayout (https://github.com/smileyborg/PureLayout) so the following constraint layout code may be unusual for you:

    #pragma mark - Init
    
    - (id)initWithStyle:(UITableViewCellStyle)style
        reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style
                    reuseIdentifier:reuseIdentifier];
    
        if (self)
        {
            [self.contentView addSubview:self.textView];
        }
    
        return self;
    }
    
    #pragma mark - AutoLayout
    
    - (void)updateConstraints
    {
        [super updateConstraints];
    
        /*-------------*/
    
        [self.textView autoPinEdgeToSuperviewEdge:ALEdgeLeft
                                        withInset:10.0f];
    
        [self.textView autoPinEdgeToSuperviewEdge:ALEdgeTop
                                        withInset:5.0f];
    
        [self.textView autoPinEdgeToSuperviewEdge:ALEdgeBottom
                                        withInset:5.0f];
    
        [self.textView autoSetDimension:ALDimensionWidth
                                 toSize:200.0f];
    }
    

提交回复
热议问题