2
votes

I need some help. I have a UITableView with custom UITableViewCell. In the Cell I have one UITextView. I need to do, when the user will type some data into UITextView. UITextView will resize from content of UITextView. I realize it in - (void)textViewDidChange:(UITextView *)textView But now I need to resize the cell also, if content size of UITextView will bigger then the current Cell.

The question is how to resize UITableView Cell by UITextView contents size.

My code bellow.

@class TableCell;
@interface RootViewController : UITableViewController{
UITableView*tableViewTest;
TableCell *tableCell;
}
@property (nonatomic,retain)IBOutlet TableCell *tableCell;
@property (nonatomic,retain)IBOutlet UITableView*tableViewTest;
@end



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
          [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
    cell=tableCell;
    self.tableCell=nil;
}
[cell setTag:indexPath.row+1];

return cell;
 }

- (void)textViewDidChange:(UITextView *)textView
{
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
NSLog(@"Change=%f",textView.contentSize.height);    
}




@interface TableCell : UITableViewCell <UITextViewDelegate>{
UITextView *textViewTest;
}
@property (nonatomic,retain) IBOutlet UITextView *textViewTest;

Thanks for help

5

5 Answers

2
votes

in UITableViewDelegate you can resize your cell height by function

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// do your stuff here like resize your cell height 
}

and after that for reflecting the change in view you need to reload table by

[self.table reloadData];

and if you have section in your table then you need not to reload whole table and avoid flickering in view you just reload that specific section of table for which you perform change in height by indexpath of that cell you can also use some other way to find the desired cell here is my style to find the cell which i need to reload

NSIndexPath *ip = [self.table indexPathForCell:cell];
[self.table reloadSections:[NSIndexSet indexSetWithIndex:ip.section] withRowAnimation:UITableViewRowAnimationAutomatic];
4
votes

Don't call reloadData, instead use [table beginUpdates] stuff

1
votes

Old question but some Auto Layout magic might help someone if still looking for the answer to this.

If everything is set up properly (Auto Layout constraints) you do not have to calculate anything yourself.

All you have to do is disable UITextView's scrolling enabled property then on textViewDidChange call tableView.beginUpdates() and tableView.endUpdates().

For a detailed explanation, check out a post I wrote which also includes a working sample project.

0
votes

You can only increase the height of custom UITableViewCell by implementation it's delegate heightForRowAtIndexPath:

When cell's textView grow bigger then cell height , you need to call reloadData method on UITableView instance and heightForRowAtIndexPath: function must return the correct height for the extended cell.

0
votes

The UITableViewDelegate protocol defines:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

Which allows you to specify the heigh of each cell. The UITextView will call the data source for every visible cell. You can trigger this by calling UITableView's reloadData