2016-10-05 3 views
1

У меня есть UITableViewCell и внутри этой ячейки у меня есть UITextView. Когда я изменю текст UITextView Я хочу изменить ячейку height real time?Могу ли я изменить высоту UITableViewCell в режиме реального времени?

Как я могу это сделать? Благодарю.

+1

Вам необходимо самостоятельно изменение размеров ячеек. Посмотрите на этот учебник https://www.appcoda.com/self-sizing-cells/ – vishnuvarthan

+0

Нет, я знаю об этом трюке. Я пытаюсь изменить ячейку представления таблицы после настройки вида таблицы. –

+0

Не могли бы вы опубликовать код того, что вы пробовали до сих пор? –

ответ

0

Если вы используете автоматический макет для расчета высоты ячеек, я думаю, вам просто нужно использовать метод reloadData() на вашем столеView.

0

Reference от: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Вам необходимо подсчитайте высоту ячейки после того, как автомакет выполнит свою часть, я имею в виду, после того, как вы создали ширину/высоту внутреннего вида на основе предоставленного текста.

Следовать решение:

Первый добавить UITableView метод делегата ниже вид контроллера,

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewAutomaticDimension; 
} 
высота

Вычислить ячейки в методе heightForRowAtIndexPath: делегата:

* ЭТО ВАЖНО * : Помните, что присваивать значениям ярлыка ячеек и textView одинаковыми, как в cellForRowAtIndexPath del egate.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *cellIdentifier = [NSString stringWithFormat:@"CustomCell%d%d",(int)indexPath.section, (int)indexPath.row]; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    // Set cell label text same like you did in `cellForRowAtIndexPath` 
    // For example 
    NSString *name = [self.dataArray objectAtIndex:indexPath.row]; 
    cell.nameLabel.text = name; 
    cell.textView.text = @"This is long text....."; 

    [cell setNeedsUpdateConstraints]; 
    [cell updateConstraintsIfNeeded]; 
    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds)); 

    [cell setNeedsLayout]; 
    [cell layoutIfNeeded]; 

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

    return height; 
} 

Теперь, если вы создали CustomCell добавить ниже ограничений в initWithStyle: метод клетки

// Other controls initialisation and creation code 
// .... 
// TextView control setup 

[self.textView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
// Above line is very important otherwise below constraint wont work 
// Because by default it uses auto size constraints 


// Other controls constraints 
// .... 

[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textView]-10-|" options:0 metrics:nil views:@{@"textView" : self.textView}]]; 

NSLayoutConstraint *pinToBottom = [NSLayoutConstraint constraintWithItem:self.textView 
                   attribute:NSLayoutAttributeBottom 
                   relatedBy:NSLayoutRelationEqual 
                    toItem:self.contentView 
                   attribute:NSLayoutAttributeBottom 
                   multiplier:1 
                   constant:0]; 
[self.contentView addConstraint:pinToBottom]; 

И добавить ниже метод делегата

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    [self.contentView setNeedsLayout]; 
    [self.contentView layoutIfNeeded]; 

    self.textView.preferredMaxLayoutWidth = CGRectGetWidth(self.textView.frame); 
} 

Но если вы еще не создали пользовательскую ячейку, и вы используя ячейку из Storyboard, добавьте нижнее ограничение в ваш superView (представление содержимого ячейки).

enter image description here

2

Основная идея этого решения заключается в вычислении высоты TextView и назначает его в ряд.

Примечание: это Swift 3 код:

class ViewController: UIViewController { 

    // array containing rows heights: 
    var rowHeights = [CGFloat]() 

    @IBOutlet weak var tableView: UITableView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     // fill it with default values of row heights 
     for _ in 1...10 { 
      rowHeights.append(44) 
     } 
    } 
} 

extension ViewController: UITableViewDataSource, UITableViewDelegate { 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return rowHeights[indexPath.row] 
    } 


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // return the actual number of your rows... 
     return 10 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TextViewTableViewCell") as! TextViewTableViewCell 

     // assgin the tag of current text view to use for determining the current cell's height 
     cell.textView.tag = indexPath.row 

     cell.textView.delegate = self 

     return cell 
    } 
} 

extension ViewController: UITextViewDelegate { 
    func textViewDidChange(_ textView: UITextView) { 

     // calculate the current height and append it in the rowHeights depends on the textView's tag. Add your the textView fontSize instead of 15 
     rowHeights[textView.tag] = (textView.text?.heightWithConstrainedWidth(width: tableView.frame.size.width, font: UIFont.systemFont(ofSize: 15)))! 

     // for updating the tableView appearence (don't use "reloadData", it will resignFirstResponder the textView) 
     tableView.beginUpdates() 
     tableView.endUpdates() 

     textView.setContentOffset(CGPoint.zero, animated: true) 

     //textView.becomeFirstResponder() 
    } 
} 

extension String { 
    // this method calculates the height of string depending on your view width and font 
    func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat { 
     let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude) 
     let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) 

     return boundingBox.height 
    } 
} 

CustomTableViewCell:

class TextViewTableViewCell: UITableViewCell { 
    @IBOutlet weak var textView: UITextView! 
} 

Вывод должен выглядеть следующим образом:

enter image description here

Надежда Это помогло.

0

Вызов Этот метод из cellForRowAtIndexPath делегатом

(CGSize)attributedText:(NSAttributedString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size 
{ 
     return [self textViewHeightForAttributedText:text constrainedToSize:size]; 
} 

Этот метод даст вам высоту вашей ячейки.

(CGSize)textViewHeightForAttributedText: (NSAttributedString*)text constrainedToSize:(CGSize)size 
    { 
     UITextView *calculationView = [[UITextView alloc] init]; 
     [calculationView setAttributedText:text]; 
     CGSize size1 = [calculationView sizeThatFits:size]; 
     return size1; 
    } 

использовать это заявление в cellForRowAtIndexPath

CGSize textSize = {tableView.frame.size.width-70, 10000.0 }; 
CGSize size1 =[self attributedText:attrString sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:14] constrainedToSize:textSize]; 
Смежные вопросы