13

我根据要显示的内容类型创建了一个UITableView不同类型的。UITableViewCell其中之一是以这种方式以编程方式创建的UITableViewCellwith inside :UITextView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  if([current_field.tipo_campo isEqualToString:@"text_area"])
  { 
    NSString *string = current_field.valore;
    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
    CGFloat height = ([string isEqualToString:@""]) ? 30.0f : stringSize.height+10;
    UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, height)];
    textView.font = [UIFont systemFontOfSize:15.0];
    textView.text = string;
    textView.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
    textView.textColor=[UIColor blackColor];
    textView.delegate = self;
    textView.tag = indexPath.section;
    [cell.contentView addSubview:textView];
    [textView release];   
    return cell;
  }
  ...
}

由于文本视图是可编辑的,包含它的单元格应更改其高度以正确适合文本视图大小。最初我通过调整UITextView方法内部的大小来做到这一点textViewDidChange:,以这种方式:

- (void)textViewDidChange:(UITextView *)textView
{
  NSInteger index = textView.tag;
  Field* field = (Field*)[[self sortFields] objectAtIndex:index];
  field.valore = textView.text;
  [self.tableView beginUpdates];
  CGRect frame = textView.frame;
  frame.size.height = textView.contentSize.height;
  textView.frame = frame;
  newHeight = textView.contentSize.height;
  [self.tableView endUpdates];
}

我将文本视图的新高度保存在一个变量中,然后当tableView:heightForRowAtIndexPath调用 : 方法时,我以这种方式调整单元格的大小:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  if ([current_field.tipo_campo isEqualToString:@"text_area"])
  {
      return newHeight +10.0f;
  }
  else
    return 44.0f;
 ...
}

以这种方式,两者都被调整大小但不同步完成,即首先TextView调整大小,然后调整单元格的高度,因此用户会立即看到文本视图大于单元格。我该如何解决这种不良行为?

4

8 回答 8

19

我为您的问题创建了一个演示,希望对您有所帮助。

我的解决方案是使用AutoResizingMask.UITextView

我的 .h 文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITabBarDelegate, UITableViewDataSource, UITextViewDelegate>{
    IBOutlet UITableView *tlbView;
    float height;
}

@end

还有我的 .m 文件(仅包括必需的方法)

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    height = 44.0;
}

- (void)textViewDidChange:(UITextView *)textView{
    [tlbView beginUpdates];
    height = textView.contentSize.height;
    [tlbView endUpdates];
}

#pragma mark - TableView datasource & delegates
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        if (height>44.0) {
            return height + 4.0;
        }
    }
    return 44.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];

    UITextView *txtView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 2.0, 320.0, 40.0)];
    [txtView setDelegate:self];
    [txtView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; // It will automatically resize TextView as cell resizes.
    txtView.backgroundColor = [UIColor yellowColor]; // Just because it is my favourite
    [cell.contentView addSubview:txtView];

    return cell;
}

希望它会帮助你。

于 2013-01-10T13:32:57.070 回答
4

要调整单元格的大小,您将使用与此类似的代码

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
    CGSize size = // calculate size of new text
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;

    if ((NSInteger)size.height != (NSInteger)[self tableView:nil heightForRowAtIndexPath:nil]) {

        // if new size is different to old size resize cells. 


        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    }
    return YES;
}
于 2013-01-08T08:09:23.833 回答
2

Set the TextView Frame With An Animation ..so that it syncs with the cell's animation of expanding height

于 2013-01-10T10:24:27.647 回答
1

我不会使用方法为细胞高度而烦恼

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

而是让您的视图文本字段委托并以如下所示的方式处理以下事件:

- (void) textFieldDidResize:(id)sender
{
          [self.tableView beginUpdates];
          [self.tableView endUpdates];
}

还要确保您执行了以下操作:

yourInputField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

然后,您唯一需要做的就是调整文本字段的大小。表格视图中的单元格将采用内部文本字段的大小。只需将其作为子视图添加到 cell.contentView。

于 2013-01-12T11:24:04.540 回答
1

检查一下:UIView Contentmode - 使用以下值:

cell.contentMode = //...//
于 2013-01-05T17:45:47.557 回答
1
- (void)textViewDidChange:(UITextView *)textView
{
    UITableViewCell *cell = (UITableViewCell*)textView.superview.superview;

    if (cell.frame.size.height < textView.contentSize.height) {
        [self.tableView beginUpdates];
        CGRect frame = textView.frame;
        frame.size.height = textView.contentSize.height;
        textView.frame = frame;
        CGRect cellFrame = cell.frame;
        cellFrame.size.height = textView.frame.size.height;
        cell.frame = cellFrame;
        [self.tableView endUpdates];
    }

}
于 2013-01-05T22:01:26.627 回答
1

Siba Prasad Hota 的代码可能会解决问题(您需要从单元格级别引用表格视图),但我有另一种更长的方法。我总是以这种方式做这些事情,因为我喜欢把所有东西都分开(MVC 模式)。如果我是你,我会这样做(来自头部的代码):

细胞父协议:

@protocol CellParent <NSObject>
@required
@property (nonatomic, strong) UITableView *tableView;
@end

电池型号:

@interface CellModel
@property (nonatomic, assign) BOOL hasTextView;
@property (nonatomic, strong) NSString *textViewContent;
-(float)getCurrentHeightForCell;//implement calculating current height of cell. probably     2 * SOME_MARGIN + height of temporary textView with textViewContent variable

细胞

@interface MyCell
@property (nonatomic, strong) CellModel *dataModel;
@property (nonatomic, weak) id<CellParent> parent;
@property (nonatomic, strong) UITextView *textView;
- (id)initWithStyle:(UITableViewCellStyle)style andModel:(CellModel*) model;

像这样的实现:

(id)initWithStyle:(UITableViewCellStyle)style andModel:(CellModel*) model
{
    self = [super initWithStyle:style reuseIdentifier:@"MyCell"];
    if (self) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        self.dataModel = model;
    }
    return self;
}


-(void) setDataModel:(CellModel *)dataModel
{
    _dataModel = dataModel;
    if(_dataModel.hasTextView)
    {
        //show text view
    }
    else
    {
        //hide text view
    }
    //do other cell modifications
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{
    self.dataModel.textViewContent = textView.text;
    [self.parent.tableView beginUpdates];
    [self.parent.tableView endUpdates];
    return YES;
}

带表格视图的控制器

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil) 
    {
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault andModel:          [self.cellsModels objectAtIndex:indexPath.row]];
    }
    cell.dataModel = [self.cellsModels objectAtIndex:indexPath.row];
    cell.parent = self;
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    return [((CellModel*)[self.tableContentArray objectAtIndex:indexPath.row]) getCurrentHeightForCell];
}
于 2013-01-08T14:30:47.880 回答
1

You should calculate newHeight for cell before loading cell. Instead of calculating newHeight in textViewDidChange, calculate it in heightForRowAtIndexPath and return same as

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if ([current_field.tipo_campo isEqualToString:@"text_area"])
  {
      NSString *string = current_field.valore;
      CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
      CGFloat height = ([string isEqualToString:@""]) ? 30.0f : stringSize.height+10;

      return height + 10.0f;
  }
  else
  {
      return 44.0f;
  }
}
于 2013-01-09T15:15:52.850 回答