HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 增加uitableviewcell高度同时增加内部的UITextView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
根据要显示内容类型,创建一个具有不同类型的UITableViewCell的UITableView.其中一个一个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 *textV=[[UITextView alloc] initWithFrame:CGRectMake(5,5,290,height)];
        textV.font = [UIFont systemFontOfSize:15.0];
        textV.text = String;
        textV.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
        textV.textColor=[UIColor blackColor];
        textV.delegate = self;
        textV.tag = indexPath.section;
        [cell.contentView addSubview:textV];
        [textV release];   
        return cell;
      }
      ...
    }

此文本视图是可编辑的,以便包含它的单元格和TextView相同的单元格必须调整大小.最初我通过这样调整textViewDidChange:中的TextView的大小来实现这一点:

- (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调整大小,然后调整单元格的高度,从而立即看到文本视图大于单元格.如何解决这个问题?

@H_674_14@

解决方法

我已经为您的问题创建了一个演示,希望会帮助您.

我的解决方案是使用UITextView的AutoResizingMask.

我的.h@L_450_15@

#import <UIKit/UIKit.h>

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

@end

和我的.m@L_450_15@(只包括必需的方法)

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

希望它会帮助你.

@H_674_14@ @H_674_14@

大佬总结

以上是大佬教程为你收集整理的ios – 增加uitableviewcell高度同时增加内部的UITextView全部内容,希望文章能够帮你解决ios – 增加uitableviewcell高度同时增加内部的UITextView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。