Button not responding in Table View Cell [duplicate] - ios

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.

Related

Removing a static UIView from a UITableView with Prototype Cells

I have a storyboard I'm maintaining that is a UITableViewController.
The UITableView is set to use Dynamic Prototypes and has 2 cells.
Above those two cells is a UIView inserted within the table view. That view contains a UIButton and another custom UIView. Both have constraints. The effect is that we have a table view with a button above it.
Now, in certain conditions, we want to remove that button and have the rest of the table view move up in the UI. I can remove that button -- by itself or by removing that parent UIView, but I cannot seem to to get the table to change the position of the prototype cells.
To hopefully make it clear, my document outline looks like this:
Table View
View (buttonContainerView mentioned below)
UIButton
View (child view mentioned below)
Constraints
Prototype Cell 1
Prototype Cell 2
I have tried several different approaches to remove the button. After reading that removing it from the superview will remove constraints as well, that's the approach I currently have in place.
[self.buttonContainerView removeFromSuperview];
I've tried removing the button, the child view, and the container view. I've also tried explicitly removing the constraints then removing the views. None of these seemed to work.
I've also tried the above with calls to setNeedsLayout and setNeedsUpdateConstraints on the table view, as well as setNeedsDisplay on the view.
I've also tried setting the row height to 0 for that first view, but it turns out it's not a UITableViewCell, so that doesn't work, either.
Of course, if I delete that container view from the storyboard, the prototype cells do appear in the UI exactly where they should.
Can you sense the flailing about?
What else could I be missing here to remove that top view effectively?
What I didn't realize is that the setup I have is a UITableView with a table header. To remove it and fix the spacing problem I was having I just needed to set self.tableView.tableHeaderView = nil in viewWillAppear:.

keep keyboard from hiding UITextField at bottom of UITableView

According to the answer at UITableView, make footer stay at bottom of screen?, which I verify:
In order to have a footer that stays put at the bottom of the screen,
and doesn't scroll with the table, then you can't use a table view
footer. You can't even use a UITableViewController, you'll need to
implement your view controller as a UIViewController. Then you add
your own table view as a subview. You'll also need to add your footer
as a subview of the view controller's view, not the table view. Make
sure you size the table view so its bottom is at the top of the footer
view.
but the problem is that my UITextField, inside my footer, is being hidden by the keyboard when user tries to type. So how do I keep the keyboard from hiding the UITextField? Throughout the app, I have been using TPKeyboardAvoiding. But in this case, where a UIScrollView/TPKeyboardAvoidingScrollView contains a UITableView and a UIView in vertical order, it does not work. I generally like TPKeyboardAvoiding because it’s so quick and easy. Any ideas how I might fix this issue?
The instruction that you quote is telling you to use a UIViewController, add a UITableView as a subview, then add a UITextField as a subview of the UIViewController NOT as a subview of the UITableView.
Let me draw it out for you:
Step 1:
Add a UITableView to your view controller
Step 2:
Add your UITextField subview (you can embed the UITextField in a UIView container if you like) as a subview of your UIViewController's view NOT your UITableView footer.
Step 3:
If depending on if your using frames or autolayout, you adjust the frame or autolayout constraint constant values for your UITextField subview when user tap on the text field.
Step 4:
Finally, the keyboard appears and the UITextField doesn't get obscured by the keyboard.
Just make sure your UITableView doesn't initialize it's own automatic scrolling for keyboard appearing. If you use UITableViewController then just subclass it and make sure you don't call it's viewWillAppear:. I didn't investigate which exactly method you should block without using UITableViewController, so, you should find out this yourself or somebody else would help.
I did similar in my own project where I wanted to have own scrolling of table view for keyboard appearing.

How do I add a footer to a UITableView using Storyboard?

Same as this question, only the proposed solution doesn't work for me. When I drag a view to the bottom area of a tableView, it tries to add it to the list of cells higher up:
I'm sure I'm missing something simple... I'm new to storyboards.
EDIT:
Maybe it is adding a "footer" (though, it doesn't label it as such), it's just not adding it low enough. I was ultimately hoping to add an item that would appear at the bottom of the screen (and stick to the bottom of the screen).
TIP: You can use the Tree view (outline view) on the left to arrange the Views (and sub views). I have done a lot of storyboard editing, and dropping things into table views rarely go to the correct hierarchy level in the tree view.
Create a New UIViewController
Insert a UITableView
Resize the tableview by dragging its dimensions
The attached picture has a UITableView on TOP of a UIViewController's UIView. Make sure that you set the delegates in the UIViewController's .m, assign the tableView as a property of the view controller, and then set the tableview's delegate property to the UIViewController object. i.e.: tableView.delegate = self or [tableView setDelegate:self]; also with datasource.
OR you can just click the tableView, on story board, and then drag its delegate and datasource property to THE UIVIEWCONTROLLER! not the view! You can do this by dragging it to the this highlighted part of the view controller's toolbar on the storyboard:
Either you can create a footer view programatically or you can load a view from UIView outlet
UIView *tempFooter=[[UIView alloc]initWithFrame:self.Footerview.frame];
[tempFooter addSubview:self.Footerview];
self.ItemDetailsTable.tableFooterView=tempFooter;
self.Footerview //View outlet
You need to set all the needed constraints of the self.Footerview to get the required layout of the footer view.But you don't need to set constraints for tableview footer itself.

iOS prevent subview of tableview from scrolling with tableview

I have added a subview to my tableview and when ever the user scrolls the tableview, the subview scrolls with it. How do I prevent this? I know it's probably along the lines of not adding the view to the tableview's subviews, but I have no knowledge of any other ways to do this. Thanks.
If you want to make a view a subview of the table view, then you can make it floating (non-scrolling) by changing its origin.y value in the scrollViewDidScroll method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.iv.frame = CGRectMake(self.ivOrigin.x, self.ivOrigin.y + self.tableView.bounds.origin.y, self.iv.frame.size.width, self.iv.frame.size.height);
}
In this example, "iv" is a property for an image view, and "ivOrigin" is a property for the initial origin of the image view (defined when I created the image view and its frame in viewDidLoad).
The UITableView is built and intended to be a view of things that scroll.
So, you can either fight that, which as you're discovering is quite hard since everything about the component is built and focused around scrolling and fast display of a subset of the full list data... Or, you can not fight it and put your static item on top of the table as a fixed-position item.
If there's a reason you can't add the table view and your animate-out item in your main view, you can always add a custom UIView class that contains both the table view and your animated view. Have your custom view class expose the contained table view as a .table property, and the container you're putting things in can be tweaked to use "mycontainerObject.tableview" instead of just "tableview" where needed.
Yes, it's a little more work to write the custom UIView subclass and give it a couple properties to hold the UITableView and whatever UIView you're animating out.. but it's likely a lot safer in the long run than trying to "hack" into the UITableView's methods and view hierarchy to try to give it a "fixed in place" behavior.

uiview on top of uitablecell

I have a custom table view with a custom table view cell.
I have a uiview with three buttons. How can I set that when a row is selected the uiview will popup over the cell? Basically I want a popover controller. Should the uiview be inside by table view controller?
I'm targeting iphone and cannot use people's project.
It depends on whether your popover view fits entirely within the cell.
If so, when you build the cell, add the popover view and mark it hidden. Then, when the delegate is told that the cell has been selected, change hidden to NO (you could even play with the alpha and wrap that inside [UIView animateWithDuration:animations:] to have it fade in).
If not, then you'll want to have the popover view ready ahead of time, and add it to the window directly when the cell is selected. You'll want to use the frame of the cell and the [cell convertRect:to:] method to position your view appropriately. You will also want to make sure that you add a "background" UIView to ensure that stray touches can't reach the UITableView underneath.

Resources