How can a create a automatically resizing table cell for entering texts like in the iPhone's “mail” app?

后端 未结 3 837
梦谈多话
梦谈多话 2021-02-09 16:59

I\'m try to emulated something just like the \"new message\" page in Apple\'s mail app on the iphone. I\'ve implemented it with a tableview and I\'ve successfully gotten the \"T

相关标签:
3条回答
  • 2021-02-09 17:28

    You could try my answer to a question similar to this...the key is to use

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    

    To do this without reloading the data.

    First off, of course, you're going to want to create your UITextView and add it to your cell's contentView. I created an instance variable of UITextView called "cellTextView" Here is the code that I used:

    - (UITableViewCell *)tableView:(UITableView *)tableView fileNameCellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    
        if (!cellTextView) {
            cellTextView = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 5.0, cell.bounds.size.width - 30.0, cell.bounds.size.height - 10.0)]; // I use these x and y values plus the height value for padding purposes.
        }
        [cellTextView setBackgroundColor:[UIColor clearColor]];
        [cellTextView setScrollEnabled:FALSE];
        [cellTextView setFont:[UIFont boldSystemFontOfSize:13.0]];
        [cellTextView setDelegate:self];
        [cellTextView setTextColor:[UIColor blackColor]];
        [cellTextView setContentInset:UIEdgeInsetsZero];
        [cell.contentView addSubview:cellTextView];
    
        return cell;
    }
    

    Then, create an int variable called numberOfLines and set the variable to 1 in your init method. Afterwards, in your textViewDelegate's textViewDidChange method, use this code:

    - (void)textViewDidChange:(UITextView *)textView
    {
        numberOfLines = (textView.contentSize.height / textView.font.lineHeight) - 1;
    
        float height = 44.0;
        height += (textView.font.lineHeight * (numberOfLines - 1));
    
        CGRect textViewFrame = [textView frame];
        textViewFrame.size.height = height - 10.0; //The 10 value is to retrieve the same height padding I inputed earlier when I initialized the UITextView
        [textView setFrame:textViewFrame];
    
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    
        [cellTextView setContentInset:UIEdgeInsetsZero];
    }    
    

    Finally, paste this code into your heightForRowAtIndexPath method:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        float height = 44.0;
        if (cellTextView) {
            height += (cellTextView.font.lineHeight * (numberOfLines - 1));
        }
        return height;
    }
    
    0 讨论(0)
  • 2021-02-09 17:30

    Mail.app doesn't seem to use UITableView. It looks like there custom items (labels and text fields) with UITextView on bottom.

    0 讨论(0)
  • 2021-02-09 17:38

    I would recommend using a UIScrollView yourself instead of a UITableView. UITableView isn't really built to support such a thing.

    0 讨论(0)
提交回复
热议问题