iOS 8 auto-sizing UITableViewCell with UITextView

前端 未结 6 1524
余生分开走
余生分开走 2021-02-12 12:11

iOS 8 introduced a way for tableViews to automatically adjust their cell\'s height based on their content (via AutoLayout).

// in viewDidLoad:
tableView.rowHeigh         


        
6条回答
  •  感动是毒
    2021-02-12 12:57

    This is the function I use, which solves the problem of the table view bouncing back after every keystroke.

     - (void)textViewDidChange:(UITextView *)textView {
    
        CGFloat fixedWidth = textView.frame.size.width;
        CGSize oldSize = textView.frame.size;
        CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
            // Resize cell only when cell's size changes, to prevent bouncing back and forth.
            if (oldSize.height != newSize.height) {
            [UIView setAnimationsEnabled:NO];
            CGRect newFrame = textView.frame;
            newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
            textView.frame = newFrame;
            [self.tableView beginUpdates];
            [self.tableView endUpdates];
            [UIView setAnimationsEnabled:YES];
            }
        }
    

提交回复
热议问题