3

I have a UITextField that I want to create a custom class on. So I created a file with a subclass of UITextField. Next, in the custom class, I want to implement a tableView. Kind of like a auto-complete textField.

I started creating it, and added the tableView like this:

[self addSubview:self.tableView];

When I run the app, the tableView is in the textField, so I can only see part of the tableView. How can I add it as a subview so I can see the full tableView?

Jessica
  • 9,379
  • 14
  • 65
  • 136
  • I guess you want to customize UITableView actually, because I can't see that you are using a customized UITextField. And try to explain your requirement clearly in question. – Mrunal Jun 24 '15 at 06:48
  • I'm pretty sure that UITextField is not the kind of class that handles subclassing very well. I would suggest you to use composition over subclassing. Create your own UIView subclass with UITexField and UITableView as subviews. – Andrea Jun 24 '15 at 06:58
  • look at http://stackoverflow.com/questions/16686379/textview-with-suggestions-list-in-ios – Saurabh Prajapati Jun 24 '15 at 07:00

2 Answers2

1

This is what you are looking for https://github.com/gaurvw/MPGTextField

This uitextfield subclass does what you want - it's builed for 'search' feature. If you still want to use your own, add tableview not to uitextfield itself, but like

[[self superview] addSubview:tableViewController.tableView];

EDIT:

you can set frame as:

 CGRect frameForPresentation = [self frame];
 frameForPresentation.origin.y += self.frame.size.height;
 frameForPresentation.size.height = 200;
 [tableViewController.tableView setFrame:frameForPresentation];

The way to add subview to uitextfield is to overload layoutSubviews method and init your tableview there:

- (void)layoutSubviews 
{ 
[super layoutSubviews]; 
if (!self.tableview.superview) 
{ 
[self setupView]; 
} 
}
Doro
  • 2,413
  • 2
  • 14
  • 26
  • I tried that without a tableViewController, just a tableView. When I run the app, I don't see any tableViews – Jessica Jun 24 '15 at 07:14
  • how do you set frame of your tableview? – Doro Jun 24 '15 at 07:17
  • CGRectMake(0, 0, 100, 300) – Jessica Jun 24 '15 at 07:18
  • try to use uitextfield frame as basic frame for your tableview, as i mention on my edit – Doro Jun 24 '15 at 07:19
  • I don't think the problem is in the frame, because i do see part of it when I do self addSubiew. – Jessica Jun 24 '15 at 07:22
  • Also, like @liushuaikobe said, I inserted self.clipsToBounds = NO; and I see the whole thing, but I just can't select the cells under the textField – Jessica Jun 24 '15 at 07:23
  • heh, the problem is that tableview will always cut by textfield frame. add it to [self superview] addSubview: with correct frame. Does this work? – Doro Jun 24 '15 at 07:24
  • I tried that, and insertSubview: aboveView, and both didn't show any tableView – Jessica Jun 24 '15 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81366/discussion-between-doro-and-jessica). – Doro Jun 24 '15 at 07:31
0

This will add the tableView as the subView of the textField.

self.tableView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), YOUR_TABLE_HEIGHT);
[self addSubview:self.tableView];
self.clipsToBounds = NO;

However, a better way is to make the tableView as the textField's superView's subView, that is, the textField and the tableView should be siblings.

liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
  • Thanks for the answer!! When I do `self.clipsToBounds = NO;` then it shows the tableView as it should, but the cells under the textField aren't clickable. – Jessica Jun 24 '15 at 07:05
  • @Jessica Then why not try a better way? make the `textField` and the `tableView` siblings. – liushuaikobe Jun 24 '15 at 07:25
  • What do you mean siblings!! Sorry... I'm a newbie – Jessica Jun 24 '15 at 07:26
  • @Jessica Add the `tableView` to the `textFiled`'s superView's subView rather than the `textFiled`'s subView. – liushuaikobe Jun 24 '15 at 07:27
  • When I do [`self.superview addSubview:self.tableView]`, I don't see the tableView – Jessica Jun 24 '15 at 07:28
  • @Jessica Because the frame of the `tableView` is not correct. Try to set the frame of the `tableView` to `self.tableView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, 300);` Then, `[self.superView addSubview:self.tableView]` – liushuaikobe Jun 24 '15 at 07:31
  • I don't think the tableViews frame isn't the problem, because when self.clipsToBounds is NO, I see it, but can't select it – Jessica Jun 24 '15 at 07:35