UITextView extension inside UITableViewCell

There are over a dozen questions about SO that are similar, but there seems to be no consensus on the best way to do this. Also, I did not find a solution that worked for me. What is different from my custom UITableViewCell is that I have two labels, one of which is in its own view and a text view. All three of them should expand to their contents.

I am wondering if my restrictions are configured incorrectly, but I'm not sure what to configure.

Here is my cell in IB.

cell in IB

The content view is light red; the optional top view is white with a label inside it.

When changing the text, I save the text and update tableView:

-(void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"row: %i", textView.tag);
    [self.textStrings setObject:textView.text atIndexedSubscript:textView.tag];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

tableView:heightForRowAtIndexPath: , . , systemLayoutSizeFittingSize:. , -, .

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // use cached height if there is one (and it isn't the selected row)
    if ([self.selectedRow integerValue] != indexPath.row && indexPath.row < [self.rowHeights count]){
        return [[self.rowHeights objectAtIndex:indexPath.row] floatValue];
    }

    static NSString *CellIdentifier = @"RMTextViewCell";
    RMTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    [self configureCell:cell forIndexPath:indexPath];

    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    height += 1; // add 1 for border

    NSNumber *h = @(height);
    [self.rowHeights setObject:h atIndexedSubscript:indexPath.row];

    return height;
}

TextView , , . / textView . , , .

?

Github . ViewControllers/RMTextViewTableViewController Cell/RMTextViewCell. , " ".

+3
3

. UILabel UITableViewCell , UITextView UITableViewCell .

CGSize textViewSize = [cell.textView sizeThatFits:CGSizeMake(cell.textView.frame.size.width, FLT_MAX)];
+1

, iOS, , UITextView UILabels, contentView , systemLayoutSizeFittingSize. , user501836, textView . , contentView. , View: heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    CGSize textViewSize = [[cell textView] sizeThatFits:CGSizeMake([[cell textView] frame].size.width, FLT_MAX)];

    height += textViewSize.height;

    return height;

}
+2

, iOS7, iOS8. jobb , UITextView.contextSize vs. UITextView.frame.size.

I also found that in xCode 6 it seemed that systemLayoutSizeFittingSize was respecting the UITextView textContainerInsets. Thus, I found that they set these values ​​to zero before adding extra height to systemLayoutSizeFittingSize in order to better set the interval.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get the cell once and save it in an NSDictionary. Thisupports cases with multiple cells types.
    CCTextViewCell *sizingCell = [self.offscreenCells objectForKey:CellIdentifier];
    if (!sizingCell) {
        sizingCell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        [self.offscreenCells setObject:sizingCell forKey:CellIdentifier];
    }

    [self populateCellViewWithData:sizingCell atIndexPath:indexPath];

    sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(sizingCell.bounds));
    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    // UITextViews don't autosize well with systemLayoutSizeFittingSize.
    // We need to add the sum of the heights of the UITextViews without its textContainerInsets to the height.
    // We also need to set the textContainerInsets to zero so that systemLayoutSizeFittingSize give the right number.
    // So we do this first.
    CGFloat additionalUITextViewsHeight = 0;
    for (UIView *subVw in sizingCell.contentView.subviews) {
        if ([subVw isKindOfClass:[UITextView class]]) {
            UITextView *subTextView = (UITextView*) subVw;

            subTextView.textContainerInset = UIEdgeInsetsZero;
            CGSize textViewSize = [subTextView sizeThatFits:CGSizeMake(subTextView.contentSize.width, FLT_MAX)];
            additionalUITextViewsHeight += textViewSize.height;
        }
    }

    CGFloat height = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + additionalUITextViewsHeight;

    return height;
}
0
source

All Articles