增加uitableviewcell高度同时增加内部UITextView

我根据要显示的内容的types创build一个具有不同types的UITableViewCell 。 其中之一就是一个UITableViewCell ,里面有一个以这种方式编程创build的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 *textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, height)]; textV.font = [UIFont systemFontOfSize:15.0]; textV.text = string; textV.autoresizingMask = UIViewAutoresizingFlexibleWidth; textV.textColor=[UIColor blackColor]; textV.delegate = self; textV.tag = indexPath.section; [cell.contentView addSubview:textV]; [textV release]; return cell; } ... } 

此文本视图是可编辑的,以便包含它的单元格和TextView相同的单元格必须resize。 最初,我通过在方法textViewDidChange调整TextView的大小来做到这一点:

 - (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]; } 

我将文本视图的新高度保存在variables中,当调用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; ... } 

这样既resize,但不同步,即首先resize,然后调整单元格的高度,导致立即看到文本视图大于单元格。 我怎样才能解决这个问题?

我已经为你的问题创build了一个演示,希望能帮助你。

我的解决scheme的想法是使用UITextView AutoResizingMask

我的.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; } 

希望它能帮助你。

要调整单元格大小,您可以使用类似于此的代码

 - (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; } 

使用animation设置TextView Frame,使其与单元格展开高度的animation同步

检查了这个: UIView内容模式 – 玩像下面这样的值:

 cell.contentMode = //...// 
 - (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]; } } 

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]; } 

加载单元格之前,您应该为单元格计算newHeight。 而不是计算textViewDidChange中的newHeight,计算它在heightForRowAtIndexPath并返回相同的

 - (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; } } 

我不会打扰使用方法的细胞高度

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

而是使您的视图文本字段委托,并以下面显示的方式处理以下事件:

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

另外请确保你做了以下事情:

 yourInputField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

那么,你唯一需要做的就是调整文本字段的大小。 tableview中的单元格将采用内部文本字段的大小。 只需将它作为子视图添加到cell.contentView。