我創建一個UITableView
與不同類型的UITableViewCell
取決於要顯示的內容的類型。其中之一,這是一個UITableViewCell
以這種方式與程序創建一個UITextView
內:同時增加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 *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;
}
...
}
由於文本視圖是可編輯包含它應該改變其高度正確地安裝文本視圖大小的單元格。 ,以這樣的方式:
- (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
:方法被調用時,我調整在小區最初,我通過調整UITextView
方法textViewDidChange
內這樣做這種方式:
- (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背景設置爲clearColor。它是透明的,錯誤是無形的。 – LuckyStarr
每次cellForRow ...被調用,你都在分配,初始化和添加UITextView作爲子視圖。爲什麼不通過創建你自己的UITableViewCell子類來做這個設置?看起來像你現在這樣做的方式會導致問題,因爲每次該方法被調用時,例如在reloadData上調用該方法時,都會向單元添加多個UITextView。 – ozz
@cdo我注意到這個錯誤,所以我修改了代碼。如果單元格爲零,則創建UITextView,否則將其恢復爲單元格的子視圖。 – LuckyStarr