Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView inside UITableView

I know this question has been asked before, though I can't seem to find what I want. I have a section in my app where I have a tableview with a textview inside of it. I DO NOT want to have a seperate .xib, .h, and .m files for the tableview cell. The tableview does not need to shrink or grow depending on the amount of text inside the textview. I don't want the textview to be editable either. I hope this isn't too much to ask for, though I'm really stuck at the moment.

like image 720
Andy B Avatar asked May 13 '11 20:05

Andy B


People also ask

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

How do you add a row to a table view?

The easiest way to add a new row to a UITableView would be to add a new item to the data and then call the reloadData method on the `UITableview. This will add a new row to our UITableView because we have added mercury to our data and then we called reloadData .


1 Answers

To do this, you will need to embed one in your UITableViewCell. But there's no need to create a custom cell. Here is the basic idea of what you will 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 want the standard 44pt height that comes with the cell. And if you want actual cells, you'll need to add your own logic so that only the cell you want is a textView, but this is the basic idea. The rest is yours to customize to your fitting. Hope this helps

EDIT: to bypass the textView to get to your cell, there are two ways to go about this.

1) you can make a custom textView class and overwrite touchesBegan to send the message to super:

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

this will send the touch events to its superview, which would be your tableView. Considering you didn't want to make custom UITableViewCells, I imagine you probably don't want to make a custom textView class either. Which leads me to option two.

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

In your code, you will want to insert a textView delegate method and we'll do all our work from there:

EDIT: changing this code to use with a UITableViewController

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// this method is called every time you touch in the textView, provided it's 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's 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 that's not what you wanted, just let me know and we'll get you sorted out

like image 161
justin Avatar answered Sep 28 '22 01:09

justin