2

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

Anton
  • 129
  • 2
  • 15

5 Answers5

4

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

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
2

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];
Anurag Soni
  • 1,077
  • 10
  • 14
1

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.

Jure
  • 3,003
  • 3
  • 25
  • 28
0

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.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • If I reload data in textViewDidChange after first letter typing the table view reload and over typing session. But this is not a problem. we can reload from textViewDidEndEditing method. The method heightForRowAtIndexPath: loading when table loading in begining of the application. How can I verify empty cell? – Anton Jul 04 '11 at 16:21
  • 1
    you're going to want to maintain the height of each cell as part of the model, and you may only want to call `- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation` i would be worried that reloading the table view might cause the key field to resign, you'd have to try it out to see – bshirley Jul 04 '11 at 17:33
  • @bshirley : Agree with you but not sure about `reloadRowsAtIndexPaths:` would cause to invoke `heightForRowAtIndexPath:`. – Jhaliya - Praveen Sharma Jul 04 '11 at 17:55
  • it may very well not, i tried to resize cells when toggling between editing and not editing, but the height method is not called at that point (realistically i just needed to make the cell larger in general, so that's what i did) – bshirley Jul 04 '11 at 18:04
0

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

Rog
  • 17,070
  • 9
  • 50
  • 73