ios – 增加uitableviewcell高度同时增加内部的UITextView

ios – 增加uitableviewcell高度同时增加内部的UITextView,第1张

概述根据要显示的内容类型,创建一个具有不同类型的UITableViewCell的UITableView.其中一个是一个UITableViewCell,内部是以这种方式编程的一个UITextView: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 根据要显示的内容类型,创建一个具有不同类型的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调整大小,然后调整单元格的高度,从而立即看到文本视图大于单元格.如何解决这个问题?

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

我的解决方案是使用UITextVIEw的autoResizingMask.

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

希望它会帮助你.

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1113505.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存