在UITableViewCell AutoLayout中的UITextView对齐问题

时间:2015-08-06 06:12:21

标签: ios objective-c uitableview autolayout uitextview

我正在设计一个看起来像这样的iOS屏幕: enter image description here

我想做什么:

  1. 用户开始编辑时应该是文本视图的文本大小。编辑完成后,它应该恢复到正常大小。

  2. 当用户正在编辑textview时,文本视图的全文应该是可见的,而在其他情况下,最多可以看到两行。

  3. 编辑进行时,应根据文本视图的大小维护UITableViewCell的大小。

  4. 这是我的代码:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      NSString *reuseIdentifier = kRateViewCellID;
      if (!self.customCell) {
        self.customCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
      }
    
      self.customCell.productDetailLabel.text =  @"Hello the product Name goes here so you can mention it here";
      self.customCell.commentTextView.text = [testData objectAtIndex:indexPath.row];
    
      // Configure the cell for this indexPath
      self.customCell.commentTextView.font = [UIFont openSansRegularFontWithSize:12];
      self.customCell.commentTextView.textContainer.maximumNumberOfLines = 2;
      self.customCell.commentTextView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
      [self.customCell.commentTextView setScrollEnabled:NO];
      if(indexPath.row == selectedIndex) {
        self.customCell.commentTextView.textContainer.maximumNumberOfLines = 0;
        self.customCell.commentTextView.font = [UIFont openSansRegularFontWithSize:16];
        self.customCell.commentTextView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
      }
      [self.customCell.commentTextView sizeToFit];
      [self.customCell setNeedsLayout];
      [self.customCell layoutIfNeeded];
      CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
      height += 1;
    
      return height;
    }
    
    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
      textView.textContainer.maximumNumberOfLines = 0;
      textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
      textView.font = [UIFont openSansRegularFontWithSize:16];
      selectedIndex = (int)textView.tag;
      [self performSelector:@selector(scrollTextViewToBottom:) withObject:textView afterDelay:0.01];
    
      return YES;
    }
    
    - (void)textViewDidBeginEditing:(UITextView *)textView {
      [self resizeTableView];
    }
    
    - (void)textViewDidChange:(UITextView *)textView {
      [testData replaceObjectAtIndex:textView.tag withObject:textView.text]  ;
      [self resizeTableView];
    }
    
    
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
      if([text isEqualToString:@"\n"]) {
        [self.view endEditing:YES];
        selectedIndex = -1;
        [self resizeTableView];
        return NO;
      }
      return YES;
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView {
      textView.textContainer.maximumNumberOfLines = 2;
      textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
      textView.font = [UIFont openSansRegularFontWithSize:12];
      [testData replaceObjectAtIndex:textView.tag withObject:textView.text];
    }
    
    - (void)scrollToCursorForTextView: (UITextView*)textView {
      CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.start];
      cursorRect = [self.feedbackTableView convertRect:cursorRect fromView:textView];
      if (![self rectVisible:cursorRect]) {
        [self.feedbackTableView scrollRectToVisible:cursorRect animated:YES];
      }
    }
    
    - (BOOL)rectVisible: (CGRect)rect {
      CGRect visibleRect;
      visibleRect.origin = self.feedbackTableView.contentOffset;
      visibleRect.origin.y += self.feedbackTableView.contentInset.top;
      visibleRect.size = self.feedbackTableView.bounds.size;
      visibleRect.size.height -= self.feedbackTableView.contentInset.top +     self.feedbackTableView.contentInset.bottom;
    
      return CGRectContainsRect(visibleRect, rect);
    }
    
    -(void)scrollTextViewToBottom:(UITextView *)textView {
      [textView setSelectedRange:NSMakeRange([textView.text length], 0)];
      [textView setScrollEnabled:NO];
      [self scrollToCursorForTextView:textView];
    }
    
    - (void)resizeTableView {
      [self.feedbackTableView beginUpdates];
      [self.feedbackTableView endUpdates];
    }
    

    我面临的问题是,每当我在textview中更改字符时,单元格的大小会发生变化,textview和contentview bottom之间的差距会增大。

    任何想法如何解决这个问题。

0 个答案:

没有答案