2013-01-02 63 views
13

我創建一個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調整大小,然後將被重新調整單元格的高度,使瞬間的用戶看到文本視圖比單元格大。我該如何解決這種不良行爲?

+0

現在我找到一個路徑將uitextview背景設置爲clearColor。它是透明的,錯誤是無形的。 – LuckyStarr

+0

每次cellForRow ...被調用,你都在分配,初始化和添加UITextView作爲子視圖。爲什麼不通過創建你自己的UITableViewCell子類來做這個設置?看起來像你現在這樣做的方式會導致問題,因爲每次該方法被調用時,例如在reloadData上調用該方法時,都會向單元添加多個UITextView。 – ozz

+0

@cdo我注意到這個錯誤,所以我修改了代碼。如果單元格爲零,則創建UITextView,否則將其恢復爲單元格的子視圖。 – LuckyStarr

回答

20

我已經創建了一個細胞的動畫同步爲你的問題演示,希望能幫助你。

我對解決方案的想法是使用AutoResizingMaskUITextView

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

希望它會幫助你。

+0

它是我採用的解決方案。 – LuckyStarr

1

檢查了這一點:UIView Contentmode - 與像值玩法:

cell.contentMode = //...// 
+0

我試過所有的價值,但我有相同的結果。 – LuckyStarr

+0

請勿更改textview高度,只能使用heightForRowAtIndexPath和任意隨機高度來調整單元格的大小。看看textview是否遵循它。如果是,則根據需要修改heightForRowAtIndexPath以更改單元格高度。 –

+0

否UITextView不會更改。我試着用textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;結束似乎工作:如果我更改單元格的高度也TextView的更改。 – LuckyStarr

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

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

斯巴·普拉薩德·保田的代碼可能會把細胞訣竅(您需要從單元格級別引用表視圖),但我有另一種更長的方法。我總是這樣做,因爲我喜歡將所有東西分開(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]; 
} 
1

您應該計算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; 
    } 
} 
2

設置TextView的框架與動畫..所以它與擴大高度

1

使用方法

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

我不會與細胞高度的哥哥,和而提出您的看法文本字段的委託和處理的方式下面的事件如下圖所示:

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

還要確保你做了以下事情:

yourInputField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

然後,你需要的唯一一件事就是調整大小你的文本字段。 tableview中的單元格將採用內部文本字段的大小。只需將它作爲子視圖添加到cell.contentView即可。