TextField superposed in tableview - ios

I initially load a hidden tableView with some data and show this when I press a button, simulating a drop down menu.
In the same view, I have a UIButton which when pressed, programmatically creates another UIButton and a UITextField.
The problem is that when I load again, the tableView and all the textFields and buttons are superposed.
This is what happens:
My question is what can I do for keep the tableView in front of in the view.

When you do -addSubview:, the view is added as the top-most view.
Which is the reason why your newly added textFields are appearing above your tableView.
The quickest way to solve your issue would be to make the tableView the top-most view when you're planning to show it.
Example:
[yourTableView setHidden:NO];
[self.view bringSubviewToFront:yourTableView];
Assuming that self.view contains yourTableView

What you are experiencing is a cell reusability issue.
If you are adding controls on cells, when they get reused They will keep the textFields which gives you this strange juxtaposition result.
You have to take proper care of removing the textField which makes things more complex.
Solution is to subclass UITableViewCell so that you have prototype cell with textfield and another one without.

Related

Button not responding in Table View Cell [duplicate]

This question already has answers here:
Programmatically send to front/back elements created from interface builder
(2 answers)
Closed 10 days ago.
I am using a .xib for my cell in a table view. Inside the .xib, is another view (cellBackgroundView), and a button. When I run the app, and click the button, it does not respond at all. Instead, it calls the tableView's didSelectedRowAtIndexPath method which brings another view controller.
Using Xcode's Debug View Hierarchy, I discovered that I have a view overlaying the all the buttons (see pic attached: this overlaying view is highlighted). This view (called backgroundView) that is overlaying my button is a View, within a view. I have a feeling when you place a view in a view, and put a button in the initial view, the button isn't called because its below the view hierarchy.
How do I fix this issue? Is there a way to move background view to the back of the view heirarchy so that the buttons will be responsive?
Debug View hierarchy:
Structure of .xib
Two things that you could check
1) Do you have a delegate method for cell height and is the height returned correct? Unless you have Clip Subviews on for the UITableViewCell, the contents of the cell can be visible outside it's frame, but the parts that are outside the cell's frame are not registering user interactions.
2) Is some other view element higher in the hierarchy (lower in the XCode listing you posted) overlapping the button? iOS Simulators Debug -> Color Blended Layers can help spotting this.
Edit:
If I interpret the added screenshots correctly, you probably have the issue mentioned in the option 1) above. If the other elements showing in the screenshot are those listed as subviews of the Cell Background View they are mostly outside the parent view's frame and thus don't receive touch events. If the background view's frame is correct, then you might want to move the other elements as children for Feed Cell directly.
Also, the element listing suggests that you are using plain UIView as the parent element. I don't know the inner workings of your application, but if you only use this view in a UITableViewCell you might want to consider making the parent view a Table View Cell in the xib. This will reduce some bloat and allow you to configure some properties for the cell in the xib.
Maybe you forgot assign your Button to code
I assume Feed Cell is a subclass of UITableViewCell, and cellBackgroundView is the property contentView of this cell.
If so, the cells property backgroundView should be behind your cellBackgroundView (the docs say: UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.).
You could set the cells property backgroundView = nil, and see whether it is still there in the view hierarchy. If so, you do add a custom backgroundView on top of the other cells views somewhere.
To check this, you could read out the subview hierarchy of your cell in your method tableView:cellForRowAtIndexPath: with something like NSArray *svs = cell.subviews; (assuming cell is the tableViewCell) and set a breakpoint behind this statement.
svs usually contains first the UITableViewCellContentView, and above it the _UITableViewCellSeparatorView. The cells backGroundView will not be shown. You could check there your view hierarchy.
If by chance there is a custom backgroundView on top, you could - as a workaround, not a solution - bring the contentView to the front by sending to the cell bringSubviewToFront: with the contentView as argument. Then the button should respond.
In your Structure of Xib Place your button below the view that is first the view is added to superView then the Button, then your button will work.
or you can code
-(void)viewDidLayoutSubviews{
[self.view insertSubview:yourButton aboveSubview:cellBackgroundView];
}
Hope it will help.
Do you add an UIButton by code?
If so you should ensure you addSubview: into cell.contentView and not into cell.
Also you could try to apply CellBackground class to a view inside contentView, not directly to contentView.
you can use this method.
[cell.contentView bringSubviewToFront:yourButton];
after that if you want to back in background then tou can use sendSubviewToBack: method .
After adding this methods your button is not responding set the userIntractionEable of button's superview.

Disabling touch interaction of Spaces between Custom table view cell

I use custom cells of subclassing UITableViewCell in a table view. And There must be space between cells. I succesfully add the space with adding a subview that I called SpaceView into the content view. But When I touched the spaceView, it is perceived as I touched the cell. And didSelectRow method called. I tried to set spaceView's UserInteractionEnabled==NO. But It doesn't work.
So my question is; Is this the right way I used to add space between cells ? Should I try to add cells for making the space ? or If its the right way, How can I prevent calling "didSelectRowAtIndexPath" method when I touched spaceView ?
like Inder said below, u can use an UIButton to solve the issue.
1) in your custom cell: at the bottom of your custom cell add a blank UIButton with the height u actually need between cells, and customize its background color according to your needs.
2) in cellforrowatindexpath: disable button of each cell. (or you can also do that in Interface Builder of previous step)
result: u have a clear disabled button that will appear as required space between cells.
Make your spaceView as UIButton (I guess it's UIView right now)
When you add UIButton that touch will be consumed by the button thus touch won't be passed to it's parent.
OR
I'm not sure if it will work or not, you can add tap gesture to your spaceView

Can I put 2 UITextFields in the same tableViewCell?

Check out this image below, this is what I'm trying to accomplish, but I ran into some problems. The first approach I made was to have an imageView (blue square) and 2 separate tableViews within a scrollView. Each tableViewCell would have textFields inside them. The first issue was when the keyboard came into the view, I would move the view up to make sure the bottom textFields were not covered up. I then wanted to be able to scroll up and down so I could still see the top textFields as well. This didn't work because when I nested the tableView inside the scrollView, The scrollView wouldn't scroll. I was also having a problem with setting the firstResponder to pass from textField to textField on the second table only.
I then decided it would be better to create 1 tableView, and add a custom cell to hold my imageView and two textFields. My question is, is it possible to create that custom cell that will hold my imageView and two textFields? would this be a better approach?
Thanks
Wow, I think your can do it easily by UITableView. All you need to do is customising UITableViewCell.
This is possible using subviews. However, why you aren't considering UIViewController or UIScrollViewController for this design?

UISearchbar inside UITableView as the last cell hiding the keyboard

I have a UITableView in which I have several custom cells and my last cell contains a UISearchbar in it. I have wired up the appropriate delegates and referencing outlets and can get the keyboard to show up.
The problem I am facing is that the searchbar is at the very bottom of my tableview i.e. it is part of the very last cell. So even though the keyboard gets shown, the searchbar is hidden below it as I suspect it is unable to scroll to that location since its the last cell in the view.
Does anyone have any thoughts on this and how to overcome this situation.
The easiest solution is to have your view controller be a subclass of UITableViewController. This type of view controller will handle issues exactly like these.
If you cannot, you should listen to keyboard will show/hide notifications and set contentInset and scrollviewIndicatorInsets bottom of the table view to the keyboard height. Then you can scroll the specific cell into visible. When the keyboard hides, set the bottom to the previous value.
Yes....Two ways
1) either you can change the frame of whole tableView and pull the whole table up by decreasing the y position of tableView
[tableView setFrame:CGRectMake(100,100,100,100)];
or
2) You can change the contentOffset of the table to programatically scroll the tableView's last cell so that it is visible to the user.
[tableView setContentOffset:CGPointMake(0,400) animated:YES]
Hope this will help you.

Adding subviews on a UITableView

I'm working on a project were I use mostly UITableViews. The user enters data and the App gives the result of a calculation. I have two buttons: "Calculate" and "Reset". I want to add a subview containing these two buttons and have them displayed at the bottom of the screen. By Storyboards, I can add the subview, but it sticks to the button of the UITableView, not the screen.
Any idea on how to accomplish that? Should I mess with the constrains between the subview with the buttons and self.tableview.superview ?
Any help is appreciated!
You can use a standard UIViewController instead of a UITableViewController, and add a UITableView to your view controller. You can then place subviews at will.
Do not forget to connect datasource and delegate of your tableview.

Resources