增加uitableviewcell高度同时增加内部UITextView [英] increase uitableviewcell height simultaneously increasing the inner UITextView

查看:100
本文介绍了增加uitableviewcell高度同时增加内部UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用不同类型的 UITableViewCell 创建 UITableView ,具体取决于要显示的内容类型。其中一个是 UITableViewCell ,其内部以这种方式以编程方式创建 UITextView

I create a UITableView with different types of UITableViewCell depending on the type of content to display. One of this is a UITableViewCell with inside an UITextView programmatically created in this way:

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

自文本视图可编辑包含它的单元格应更改其高度以正确适合文本视图大小。最初我通过调整方法 textViewDidChange :中的 UITextView 来这样做:

Since the text view is editable the cell that contains it should change its height to correctly fit the text view sizes. Initially I did this by resizing the UITextView inside the method textViewDidChange:, in this way:

- (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 :调用方法,我以这种方式调整单元格大小:

I save the new height of text view in a variable and then when tableView:heightForRowAtIndexPath: method is called, I resize the cell in this way:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  if ([current_field.tipo_campo isEqualToString:@"text_area"])
  {
      return newHeight +10.0f;
  }
  else
    return 44.0f;
 ...
}

这样两者都会调整大小但不是同步完成,即首先调整 TextView 的大小,然后调整单元格的高度,以便用户看到文本视图大于单元格。我该如何解决这个不良行为?

In this way both are resized but is not done in sync, ie first the TextView is resized and then it is resized the height of the cell, so for an instant the user see that the text view is larger than the cell. How can I fix this bad behavior?

推荐答案

我为你的问题创建了一个演示,希望对你有帮助。

I have created one demo for your problem, hope will help you.

我的解决方案是使用 AutoResizingMask UITextView

My idea of solution is using AutoResizingMask of UITextView.

我的.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITabBarDelegate, UITableViewDataSource, UITextViewDelegate>{
    IBOutlet UITableView *tlbView;
    float height;
}

@end

和我的.m文件(仅包括必需的方法)

And my .m file (Includes only required methods)

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

希望它能帮到你。

这篇关于增加uitableviewcell高度同时增加内部UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆