UITextView inside UITableView

I know that this question was asked before, although I canโ€™t find what I want. I have a section in my application where I have a table view with text inside. I DO NOT want to have separate .xib, .h and .m files for the tableview cell. The table view does not need to be reduced or enlarged depending on the amount of text inside the text field. I do not want the text image to be edited as well. Hope this is not too much to ask, although I am really stuck at the moment.

+6
iphone uitableview uitextview
source share
1 answer

To do this, you need to embed it in your UITableViewCell. But there is no need to create a custom cell. Here is the basic idea of โ€‹โ€‹what you want to do:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UITextView *comment = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, tableView.rowHeight)]; comment.editable = NO; comment.delegate = self; [cell.contentView addSubview:comment]; [comment release]; } return cell; } 

You will of course need to set your rowHeight if you don't need the standard 44pt height that comes with the cell. And if you need real cells, you need to add your own logic, so that only the cell you want is a textView, but this is the main idea. The rest is yours to customize your fitting. Hope this helps

EDIT: To get around TextView to get into your cell, there are two ways to do this.

1) you can create your own textView class and rewrite touchhesBegan to send a super message:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; } 

this will send touch events to its supervisor, which will be your tableView. Given that you don't want to create custom UITableViewCells, I think you probably don't want to create your own textView class either. This leads me to option 2.

2) when creating a textView, delete comment.editable = NO; . We need to keep it editable, but fix it in the delegate method.

In your code, you will need to insert the textView delegate method, and we will do all our work from there:

EDIT: Modifying this code for use with the UITableViewController

 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { // this method is called every time you touch in the textView, provided it editable; NSIndexPath *indexPath = [self.tableView indexPathForCell:textView.superview.superview]; // i know that looks a bit obscure, but calling superview the first time finds the contentView of your cell; // calling it the second time returns the cell it held in, which we can retrieve an index path from; // this is the edited part; [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; // this programmatically selects the cell you've called behind the textView; [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; // this selects the cell under the textView; return NO; // specifies you don't want to edit the textView; } 

If this is not what you wanted, just let me know and we will take you apart.

+24
source share

All Articles