1

I am experimenting with putting a UITextField inside a UITableViewCell.

Now I got two working methods.

Using addSubview method:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"textFieldCell"];

UITextField *textFieldView = [[UITextField alloc] initWithFrame:CGRectMake(150, 7, 150, 30)];
[textFieldView setPlaceholder:@"Placeholder"];

[cell addSubview:textFieldView];

Using setAccessoryView method:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"textFieldCell"];

UITextField *textFieldView = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
[textFieldView setPlaceholder:@"Placeholder"];

[cell setAccessoryView:textFieldView];

In my opinion the setAccessoryView result is better looking, since the alignment is done automatically.

But my question is: It it ok to put a UITextField inside a AccessoryView? Or is there a good reason why I shouldn't do it that way?

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
Saeverix
  • 397
  • 5
  • 18
  • I tried the `accessoryView` way but couldn't widen the `textField` when loading the `tableView` in landscape orientation. – ma11hew28 Apr 04 '14 at 16:30

2 Answers2

1

I don't think it's restricted. But the accessoryView has a role in the design: to show that the cell has interactive options.
If you want to do it the correct way, you should append UIViews in cell.contentView.

pbibergal
  • 2,901
  • 1
  • 17
  • 19
  • 1
    Sounds reasonable, but didn't Apple also put `UISwitch` controls inside the Settings screen using `accessoryView`? – Saeverix Dec 03 '12 at 14:30
  • I'm not sure about that.. Didn't see the source code. But maybe you're right. As I said there is no restriction in HIG. But if I want my cell to be flexible and easily modified, I wouldn't rely on accessoryView. – pbibergal Dec 03 '12 at 14:45
  • 1
    "I'm not sure about that" err, well they're all over the Settings app: http://cl.ly/image/3A2z0l2x0n1H – Ian Dundas Jul 21 '14 at 13:19
0

There is no any restriction on it by apple, but main intention to add accessoryView property in UITableViewCell class, to be used, typically as a control, on the right side of the cell (normal state).

Following sentence taken from the apple document:

If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s normal (default) enter code herestate; it ignores the value of the accessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the right side of the cell.

According to this statement you can provideUITextField (framework-provided control) as accessoryView.

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61