根据要显示的内容类型,创建一个具有不同类型的UITableViewCell的UITableView.其中一个是一个UITableViewCell,内部是以这种方式编程的一个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相同的单元格必须调整大小.最初我通过这样调整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];
}
我将文本视图的新高度保存在一个变量中,当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调整大小,然后调整单元格的高度,从而立即看到文本视图大于单元格.如何解决这个问题?
解决方法
我已经为您的问题创建了一个演示,希望会帮助您.
我的解决方案是使用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;
}
希望它会帮助你.