increase uitableviewcell height simultaneously increasing the inner UITextView

前端 未结 8 1569
花落未央
花落未央 2021-02-02 17:13

I create a UITableView with different types of UITableViewCell depending on the type of content to display. One of this is a UITableViewCell

8条回答
  •  时光取名叫无心
    2021-02-02 17:37

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

    My idea of solution is using AutoResizingMask of UITextView.

    My .h file

    #import 
    
    @interface ViewController : UIViewController{
        IBOutlet UITableView *tlbView;
        float height;
    }
    
    @end
    

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

    Hope it will help you out.

提交回复
热议问题