0

I would like to create a table view (with style UITableViewStyleGrouped) with cells that are UITextField and UITextView, since I need both single line and multi line boxes that I can type texts in. Just to give you an idea what exactly I am looking for you can refer to "Add Event" section of iPhone's calendar app. I saw a few solutions here but specifically for the UITextView that I need for multiline text typing, I don't know how to set the frame of UITextView to make it fit perfectly in one cell of grouped style table view. Specifically you can refer again to "Add Event" section of iPhone's calendar app where you can enter the "Note".

Here is the code I have which does not give me what I'm looking for. The rectangle of the textview has a different color and does not fit in the cell

    UITextView *textViewField = [[UITextView alloc] init];
    textViewField.frame = CGRectMake(0,0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);

    [cell.contentView addSubview:textViewField];
    [textViewField release];
Dogahe
  • 1,380
  • 2
  • 20
  • 50

3 Answers3

0

If you are just looking to have the cell be a static height like the notes for 'add event', you can just create the textView with the height you want by specifying the frame then set the row it is in to the same height or slightly taller in -tableView: heightForRowAtIndexPath:

This answer may also be helpful for sizing textViews

How to make a UITableViewCell with a UITextView inside, that dynamically adjust its height, on the basis of the UITextView?

Community
  • 1
  • 1
WCByrne
  • 1,549
  • 1
  • 11
  • 16
0

From my experience, creating a dynamic text view inside a cell can be a huge headache. I ended up using the free Sensible TableView framework which worked great for me.

Matt
  • 2,391
  • 2
  • 17
  • 18
0

The width of a grouped cell inside the left and right border is 300 and not 320 which is what you'll get if you check the cell.contentView.frame.size.width.

To add a bit of padding try

textViewField.frame = CGRectMake(5,5, 290, cell.contentView.frame.size.height - 10);

The Note cell for Add Event does not have a dynamic height for the cell, however, the UITextView content height can be larger than the cell height.

bbarnhart
  • 6,620
  • 1
  • 40
  • 60
  • Yeah, I don't need the dynamic height anyway. Do you know why the TextView rectangle would have a slightly different shade of white than the containing cell? And how to fix it? The color issue did not happen with UITextField though. – Dogahe Jul 11 '13 at 12:39
  • Not sure and I'm not able to check at this moment. I imagine you could change the background color or set it to clear. – bbarnhart Jul 11 '13 at 13:03
  • Thanks bbarnhart! Yes, setting the background color to clear solved one of the issues I have. I am still looking for a way to set the frame to work both for portrait and landscape orientation. Do you know how I am supposed to set the frame of UITextView to fit for both orientations? – Dogahe Jul 23 '13 at 14:58
  • I imagine the landscape width for grouped cells is 460. – bbarnhart Jul 23 '13 at 15:12