表示するコンテンツの種類に応じて、UITableView
さまざまな種類のを作成します。UITableViewCell
これの1つは、次のようにプログラムで作成されたUITableViewCell
内部のwithです。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
サイズが変更され、次にセルの高さのサイズが変更されるため、ユーザーはテキストビューがセルよりも大きいことが一瞬でわかります。この悪い動作を修正するにはどうすればよいですか?