0

Having a UITextField in a UITableViewCell

I'm making a fill-out form (First name, Middle Init, Last Name) using table views and want to embed text fields in some of my cells. I see a lot of examples on adding a UITextField inside a UITableViewCell, like the one above. However ALL of them have hard-coded values for the text field's frame, like this

UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];

I want my text field to use the cell's frame or bounds.

To make things more interesting, my table view has four sections, and only section 1, which has 3 rows, will have text fields embedded.

To make things even more interesting, I use a popup controller to present my form.

So how can I embed my text field to fit properly inside my table view cells without using hard-coded values when setting its frame or bounds?

Thanks!

Community
  • 1
  • 1
Chris F
  • 14,337
  • 30
  • 94
  • 192

3 Answers3

5

Autoresizing masks are your friend.

cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
CGFloat optionalRightMargin = 10.0;
CGFloat optionalBottomMargin = 10.0;
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, cell.contentView.frame.size.width - 110 - optionalRightMargin, cell.contentView.frame.size.height - 10 - optionalBottomMargin)];
playerTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:playerTextField];
Joe Hankin
  • 950
  • 8
  • 15
  • You're still using hard-coded values in your CGRectMake() call though. I do NOT want to have any hard-coded values at all! – Chris F Nov 14 '12 at 02:04
  • You can change the parameters to CGRectMake() as you like, and in most cases, you're going to need some kind of constants if you want to have room to put anything else in the cell. The point is, make sure the width and height are relative to the containing superview and set the autoresizing mask to `UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight`. – Joe Hankin Nov 14 '12 at 18:43
  • I combined this answer with the answer above it to get what I want. Thanks! – Chris F Nov 15 '12 at 02:02
2

If you want to ensure the UITextField fills the contentView of a cell then you do:

cell = [tableView dequeue....];
if (!cell) {
    cell = ... // create cell
    UITextField *textField = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
    textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:textField];
}

Obviously this needs additional cell setup and something should be set to the text field's delegate. But this covers the basics of getting the text field to fill the cell's contentView. If the cell's size changes the text field will change along with it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • This ALMOST worked. I'm able to type something inside the table view cells, but it's not centered, that is, there's no top margin. Also my text fields have placeholder text in them, and with this approach, I do NOT see the text, which I do see in the other answer provided. I'm presenting my form (that has the table view) as a popover so does that complicate matters? – Chris F Nov 15 '12 at 01:40
  • Ok, I rewrote my code and am now ABLE to see the text field placeholder text, so that's my bad. However, text field is still NOT centered on the table cell, but now I can deal with this by adding offsets. So essentially, the answer is a combination of this answer and the answer below. Thank you! – Chris F Nov 15 '12 at 02:01
0

You could map it out using the storyboard editor by creating a custom nib that you then reference in cellForRowAtIndexPath

Assuming your targeting iOS5+, it's quite easy to do.

  1. setup the nib in viewDidLoad with the following
[self.tableView registerNib:[UINib nibWithNibName:@"nibname" bundle:nil]  forCellReuseIdentifier:<#(NSString *)#>]
  1. load the nib in cellForRowAtIndexPath
CellClass *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)#>];

where CellClass is the class that you've created for the cell.

This approach allows a lot of flexibility to customize the cell as well as to create cell-specific methods. (say if you use more than one cell template w/in the tableView).

codercat
  • 22,873
  • 9
  • 61
  • 85
sberley
  • 2,238
  • 1
  • 16
  • 12