13

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

4

8 に答える 8

19

私はあなたの問題のために1つのデモを作成しました、希望はあなたを助けるでしょう。

私の解決策のアイデアは、を使用することAutoResizingMaskですUITextView

私の.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;
}

それがあなたを助けることを願っています。

于 2013-01-10T13:32:57.070 に答える
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;
}
于 2013-01-08T08:09:23.833 に答える
2

高さを拡大するセルのアニメーションと同期するように、アニメーションを使用してTextViewフレームを設定します。

于 2013-01-10T10:24:27.647 に答える
1

これをチェックしてください:UIViewContentmode-次のような値で遊んでください:

cell.contentMode = //...//
于 2013-01-05T17:45:47.557 に答える
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];
    }

}
于 2013-01-05T22:01:26.627 に答える
1

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];
}
于 2013-01-08T14:30:47.880 に答える
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;
  }
}
于 2013-01-09T15:15:52.850 に答える
1

方法を使ってセルの高さを気にしない

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

むしろ、ビューテキストフィールドを委任して、以下に示す方法で次のイベントを処理します。

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

また、次のことを確認してください。

yourInputField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

次に、必要なのはテキストフィールドのサイズを変更することだけです。テーブルビューのセルは、内側のテキストフィールドのサイズに採用されます。それをサブビューとしてcell.contentViewに追加するだけです。

于 2013-01-12T11:24:04.540 に答える