简体   繁体   中英

UITextView in the cell of UITableViewStyleGrouped table view

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];

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM