Button in custom UITableViewCell not responding in iOS 7 - ios

This is more or less a continuation of this post: Button in UITableViewCell not responding under ios 7
I am having the same exact issue and have tried every suggestion in the thread. Obviously I don't own that question so I can't edit it to give more info, and thus why I am posting this question now!
Problem:
I have VC nib that I load up that has a tableview in it that I resize based on how many rows are in it. Each row is made from a custom uitableviewcell subclass using a nib file. That class/nib has 4 buttons in it. I can load this code up in iOS 6 or iOS 8 right now and it works perfectly. I don't have a iOS 7 device so I'm bound to the simulator which is at 7.1 (which is the version I'm guess the user that reported this issue was using as well given it was today). Now in the simulator, and the user's phone, I can touch/click everything else on that VC except any of the buttons in the cells. It's as if they had UserInteractionEnabled set to NO, but they don't and neither are any of their parent views (as I'll soon get into).
Tried solutions:
-Completely recreating the nib from scratch both using and not using autolayout
-Calling [self.contentView addSubview:button] in the awakeFromNib of the cell class
-Tried re-adding the buttons to the contentView at runtime with [self.contentView addSubView:button]
-Have ensured four times over that every view in the hierarchy I can find that leads to these buttons have userInteractionEnabled set to YES. (including but not limited to the tableview itself, the cell, the contentView and when I added a "parent view" to the buttons that it was set as well)
-Tried raising all the buttons with a parent view that contains nothing but the buttons
-All buttons are at the top(visually bottom) of the event stack(add and remove are the other two buttons):
-Have set the table cell selection from single to none.
-I am not overriding layoutSubviews in my cell class
-I can not move any views outside of the Content View as Interface Builder takes them completely out of the cell if I do that.
-I have tried disabling the userInteractionEnabled on just the ContentView at runtime with no change
-I tried putting in the cell creation code of the tableview [cell bringSubviewToFront:cell.button]; for the different buttons to the same result.
Hopefully Helpful Facts:
-I tried setting all of the background colors of all of the views in the hierarchy to different colors so I could visually debug it at runtime... it looked exactly as expected. No overlaps or coverings. (This was limited to only views in the cell)
-Here is all of the settings for the TableView:
-I tried to load this in the new XCode 6 to use the visual debugger but the 7.1 simulator included with it actually ran the code perfectly so I could debug it...
-Here is the dequeueing code in the VC:
NiTGroupTimeCell* cell = (NiTGroupTimeCell*)[tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
-Here is the code in the viewDidLoad of the VC to set up the cell nib with the table(it's 2 because this is the from scratch one):
[self.timesTable registerNib:[UINib nibWithNibName:#"NiTGroupTimeCell2" bundle:nil] forCellReuseIdentifier:#"GroupTime"];
-All connections were made via IB. These are all using IBAction or IBOutlet.
-I have NSLog statements in all button methods to test if they are actually called, but I have also tested with breakpoints. All are never triggered in testing.
-The only TableView delegate or datasource methods implemented are as follows:
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
-As per suggestion I took Revel to it and found that a mystery UILabel and UIImageView were in the view.... but as you can see their frames are all zeros so they shouldn't be getting in the way of anything so back to where we were I'm afraid:
UILabel frame:
UIImageView frame:
IIRC I counted this off as a Simulator bug before, but since it's happening on the user's device it must be an actual issue and it's holding up my pipeline so help would be GREATLY appreciated! Thanks in advance!
PS I'm happy to post whatever, but because of all the shifting in debugging I didn't know exactly what people would want to see and I didn't want to overload this post because I knew it was going to be long with everything else.

So apparently the issue was these lines of code(in diff format from my git diff output):
--(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
+-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
and
--(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
+-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
so yeah... seems that in iOS7+ if you have the "old" output types for these datasource methods it'll look fine but bork your tableview functionally... good times, but hey it's fixed now^^ hopefully this helps others.

I would suspect that there's an issue with your custom UITableViewCell subclass, NiTGroupTimeCell, or with the IBAction connections from the nib to the tableViewCell subclass.
I also wonder about the extra empty UILabel and UIImageView. They sound like the default properties declared in UITableViewCell.h, UIImageView *imageView and UILabel *textLabel. The fact that they're getting instantiated (and aren't nil) could be a clue as to the weird, unexpected behavior you're seeing.
Do you have IBOutlets to the UIButtons? What about changing properties (in code) for the buttons (such as background color) to make sure you actually are retaining them in the NiTGroupTimeCell.

You're saying that you tried [self.contentView addSubview:button] but your button in your first image is inside a view inside contentView. Try linking an IBOutlet to that view, then try [nameOfView addSubview];

Throwing a wild guess as it looks like you have covered pretty much everything else.
You said:
I load up that has a tableview in it that I resize based on how many rows are in it
And you posted a screen shot saying the clip subviews is off on the UITableView
Are you setting the frame of the tableview incorrectly but its showing the cells anyway due the the subviews not being clipped?
Load up reveal again and check the height of the UITableView

Not sure if it is the case here. But if you name the custom cell outlets imageView, textLabel or any of the "built-in" UITableViewCell's properties you will get weird results and behavior.

You need to check couple of things:
Make sure all the parent views of the button have User Interaction enabled.
Check AutoSizing in the size inspector and make sure the button lies inside the view so it could receive touch events otherwise touch events will get ignore. You can verify it by changing the colour of the view. Changing colour or NSLog the frame of button and parent views will help you to troubleshoot if this is the problem.
Try to make things simpler and don't addSubView programmatically, if designing from IB.
Make sure some component is not overlapping the button with a clearColor background color.

What worked for me is this:
self.contentView.userInteractionEnabled = NO;
I called this after loading my custom cell from nib / storyboard. Without the userinteractionEnabled set to NO, my buttons haven't been responding to touches somehow. My contentView has received all touches and did not forward them to my buttons.
I've seen code where
[cell bringSubviewToFront:button]
has been used as a workaround, for me it did not solve the issue.

I had a simillar issue, while adding Custom Acions based on Guestures.
I was adding Action Button on runtime in Inherited class of UITableViewCell
The bahaviour was if I add buttons outside the visible rect, after animating buttons inside, I was unable to click / tap. But in My Case I was able to Tap / Click if buttons were added in visible rect.
What Worked for me, I added those buttons in View instead of ContentView of UITableViewCell, then animated only Content View. You may try somthing simillar
-- Vishal

You've said that there are no TouchRecognizers(TapRecognizers?), but I think you should double-check that, and look not only for touch recognizers but for any recognizers in controller that uses that cell,even added to self.view/tableView.
I was recently trying to find out why cells don't select(delegate method wasn't called) only to find out that I've added 2 gesture recognizers(in code, those were necessary for other things, but I had to do that in other way) that would prevent cell selection.
Also the sign of it may be that if you hold button long enough(put breakpoint there so you can make it easier), action will fire.

Related

UIButton inside UITableViewCell not working in simulator (iOS 14)

after updating Xcode to version 12, every UIButton that added to UITableViewCell like this:
self.addSubview(someButton)
in simulator(iOS version 14) the button not working(when user tap on the button nothing happening) but in real device everything works fine. when I added UIButton to cell like this everything works fine even in simulator:
self.contentView.addSubview(receptionButton)
why this is happening? and am I doing right by adding UIButton this way (self.contentView.addSubview) to UITableViewCell?
and am I doing right by adding UIButton this way
(self.contentView.addSubview) to UITableViewCell?
YES.
That is the most correct way! You can add your subviews to self(cell itself), directly, like in your first code block, BUT, you will 100% hit a bug in the future (I did), like what you've just experienced now.
why this is happening?
This is the best explanation that I could find from Apple:
The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode.
https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview

KIF cannot tap UIButton inside UITableViewCell if it's its UITableView's only one

I am having trouble automating tapping on an UIButton that is embedded inside a UITableViewCell if that cell is the table's only one. This is in the context of UI automation with KIF.
Here my relavant call:
[tester tapViewWithAccessibilityLabel: #"LABEL"
traits: UIAccessibilityTraitButton];
// trait only specified for 2nd case below
Here is what I am observing:
If I put the accessibility label on the UITableViewCell KIF's KIFUITestActor - waitForAccessibilityElement:view:withLabel:traits:tappable: returns the UITableView, not the cell. Somehow the table seems to inherit its only child's accessibility label and lets KIF encounter it first during its recursive search.
If I put the accessibility label on the UIButton instead, KIF finds it but determines that is is not tappable (i.e. UIView-KIFAdditions -tappablePointInRect: returns NO), presumably because its mainly transparent between the thin font lines for the button's label (the tap goes to a UITableViewCellContentView instead).
A workaround might be tapping on the row by it's NSIndexPath but maybe there is still a better way to overcome the described hurdles I am facing. So how could I instruct KIF to tap a button like this with a call to tapView...?
If you are making cell in code, make sure your button is added to cell.contentView.
If you are loading cell from xib, try to send contentView to back of view hierarchy (I have not found any downsides yet for doing it) with:
[self sendSubviewToBack:self.contentView];
This is just to confirm that this workaround is applicable:
[tester tapRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0]
inTableViewWithAccessibilityIdentifier: #"IDENTIFIER"];
The "good" solution should be something like this:
UITableViewCell *cell = [tester waitForCellAtIndexPath:indexPath inTableViewWithAccessibilityIdentifier:tableAccessibilityIdentifier];
UIAccessibilityElement *element = [cell accessibilityElementWithLabel:accessibilityLabel traits:UIAccessibilityTraitButton];
[tester tapAccessibilityElement:element inView:cell];
Unfortunately it doesn't work as expected. Sometimes the element is a UIAccessibilityElementMockView and tapping it in step 3 leads means just tapping the underlying cell. Naturally the results will not be as expected.
I've managed to finally work around that by doing something more along the lines of:
MyCustomCellClass *cell = (MyCustomCellClass *)[self waitForCellAtIndexPath:indexPath inTableViewWithAccessibilityIdentifier:tableAccessibilityIdentifier];
[cell.buttonOutlet sendActionsForControlEvents:UIControlEventTouchUpInside];
Note that this means
relying less on UI interaction
having a custom class
having an outlet to the button
But hey, KIF isn't supported by Apple so it sometimes, you know, breaks :)

Issue with overlapping text: iOS7

I normally post source code and examples of UI but I think its more related to an iOS7 change and I cant see it being a code bug (yet). I would have to post so much code and UI that it would be counter productive. So here is my best non-visual description:
Since upgrading a project to iOS7 I am finding that if I put call to change a UILabel or calling setText of a UIButton in a ViewDidAppear or ViewWillAppear it puts the new text right on top of the old text.
Since developing for iOS I have never had to do anything different. If I do this:
lblMyHours.text = #"12";
It shouldnt just throw that on top of my existing label.
This especially happens inside of a UITableView where I have created an iVar for a UILabel thats in a UITableViewCell. If a user makes an adjustment to a value after clicking on a cell (it takes them to a detail screen to edit), when I pop back I have it recalculate in ViewDidAppear. In that recalculating I am resetting a label like the above. But it doesnt clear out the old.
You might be adding a new label every time you return a new cell.
You should just replace the text of the current label instead.
There are two way to achieve this, one you are already doing and as #Guilherme rightly pointed out. The other way is to create a custom tableview cell and put the UILabel property in there. As for the viewDidAppear scenario, you can create a uilabel in ther .h file (in the #interface declaration) and then initialize it in ViewDidLoad method and simple use in ViewDidAppear method without having to declare it again.
I would suggest that you follow the way I described for ViewDidAppear issue, and for the UITableView issue, search for UILabels in subview of the cell everytime cellForRowAtIndex method is called and remove it from the subview, something like this before adding a new label
for(UIView *view in cell.subviews)
{
if([view isKindOfClass:[UILabel class]])
{
[view removeFromSuperview];
}
}
Hope this helps.

iOS: Tapping on UITableView does not call didSelectRowAtIndexPath

My app uses a UITableView with a UINavigationController to show a more detailed view when a row of the table is tapped - the basic drill-down routine.
When I tap on a row, it is highlighted, but the delegate methods tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: are not called (verified using debugger).
Now here's the weird part:
There are some other table views in the app (they don't drill down) and none of them exhibit the issue.
If I tap the row rapidly and repeatedly, after many tries (10 - 20 is normal), tableView:willSelectRowAtIndexPath: and tableView:didSelectRowAtIndexPath: are called and processing continues normally.
The problem occurs only on an (any, actually) iPad running iOS 6. It works fine with iPads running iOS 5, or with any iPhone running any iOS version, 6. It also works with the iPad simulator using iOS 5 or 6.
So it seems that something is receiving the tap before the delegate methods are called. But what?
I not using any UITapGestureRecognizer, so that is not the issue.
I am not using multiple UITableViewControllers for the table, so this is also not the issue.
I was having this issue with an app when running in iOS6. The tableView:didSelectRowAtIndexPath: method was never being triggered, even though it was working before updating to iOS6.
I finally figured it out by looking at the xib and the attribute inspector for the table. In the Table View section I had the Selection option set to No Selection and Show Selection on Touch was not selected.
Changing the Selection option to Single Selection actually got this working for me again and leaving the Show Selection on Touch unselected means that I don't see any flash or change of colour when the row is selected.
I encountered a similar issue. iOS 6 appears to be much more sensitive to the slightest upward movement on a tap on a table view cell. It appears it is registering the slightest movement as a scroll request rather than a cell selection. My table view was set to scrollEnabled = NO because it is a table view with static selection values. The table view appears within a small area of a larger iPad view. If I carefully tap a cell and lift directly up, the cell is selected.
To resolve it, I changed scrollEnabled to YES and made sure the area dedicated to my tableView was larger than the actual area needed to show the table view, thus preventing it from scrolling. Not sure if this is is the cause of the issue you are experiencing, but hope it helps.
I had the same problem. The UITableView was not triggering the didSelectRowAtIndexPath method when running in iOS6, but it was working fine in iOS5.1.
I had also implemented the
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
and returned NO. In iOS5 it was all working fine, but the minute I switched to iOS6 it would cease to trigger the didSelectRowAtIndexPath. Once I removed that shouldHighlightRow method, everything worked in iOS6 again. Cheers!
The usual reason that didSeletRowAtIndexPath() doesn't get called is having a UITapGestureRecognizer on your viewcontroller with Cancels touches in view set to YES.
The correct and only sensible fix is to set Cancels touches in view to NO. Then your table row selection should work nicely.
Obviously this has some implications in your gesture handlers - you may need to add some code to your gesture handler to stop some touches going on to your views i.e.
- (IBAction)onTapGesture:(UITapGestureRecognizer *)sender {
if (sender.view == someOldViewThatINeedToDealWith) {
sender.cancelsTouchesInView = true;
}
}
I tried all the other answers posted, and although they seemed promising, they didn't solve the problem.
I've since discovered the cause of problem in my case: I was calling [self.tableView reloadData] on a different thread (due to handling an NSNotification event posted from a background worker thread).
I fixed the problem by changing [self.tableView reloadData] to
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
Hope this helps.
The other suspect could be a Tap Gesture Recognizer set to the view controller's view swallowing all taps like a blackhole. Remove the gesture recognizer will enable did select. I just had the same issue and the tap gesture was the cause.
The resolution is really weird: the UITableViewController registered for all notifications. In the handler for the notifications, the table view data was being reloaded using
[self.tableView reloadData]
According to Apple DTS, when the table reloads data, this causes a notification to be sent to the observer, causing a race condition ...
No explanation for why it would work sometimes or why it would always work on an iPhone. But registering only for a small subset of notifications (i.e. the ones I was really interested in) fixed the problem!
Setting
recognizer.cancelsTouchesInView = NO;
takes care of it if your use case allows simultaneous handling
of touches by [tap] gesture recognizer (kdb dismiss for example)
and the view the recognizer is attached to.
in my case I hade button which received all touch events before tableviewcell

disable scrolling in a UITableView (iPhone SDK 3.0)

I'm trying to disable scrolling in a UITableView when editing a UITextField embedded in a UITableViewCell.
This is just to prevent the cell from being scrolled out of sight when edited (and also to avoid some related cell "Recycling" problems).
While googling around I've seen that somebody suggested the obvious:
tableView.scrollEnabled = NO:
or even
tableView.userInteractionEnabled = NO;
This does not work though (at least for me... iPhone SDK 3.0, tried on simulator)
I set these properties to NO, I even check by logging that the properties are set to NO, but the UITableView keeps on responding normally to touch events.
And it also happily scrolls.
I wouldn't be that worried if somebody on the net were not claiming that this actually works.
Am I missing something?
Or is the only alternative subclassing UITableView to make a functionality available in its superclass (UIScrollView) work again?
Did you try using
self.tableView.scrollEnabled = NO;?
I've often tried that code from the web didn't work, simply because of a lack of the prefix self. I just tried this out without a problem.
I don't know if this work when turning it on and off dynamically. It does at least work for permanent settings when initializing the object...
If you're using UITableViewController, you also have a tableView property, with no casting needed. This works for me:
self.tableView.scrollEnabled = NO;
Let me know if that works for you.
Did you try on storyboard unselect scrolling enabled?
I tried:
[(UIScrollView*)[self view] setScrollingEnabled:NO];
and it worked ([self view] is my view of the current view controller, i.e., a UITableView).
The thing is, I get a warning:
'UIScrollView' may not respond to '-setScrollingEnabled:'
In all honesty, the property is "scrollEnabled", but it works nonetheless with the aforementioned code!
So, the "right" way to do things, should be:
[(UIScrollView*)[self view] setScrollEnabled:NO];
Why it also works the other way, is confusing me...
None of these answers worked in my case. Table view kept scrolling ever though every scrollView was disabled.
Finally, I've found solution in here, claiming that UITableViewController does this "for me" whenever keyboard hides the UITextView being edit.
Solution is to inherit from UIViewController instead of UITableViewController and implement the required table functionality myself.
if you want to scroll only if its content is not visible then set:
yourTableview.alwaysBounceVertical = NO;
Here if your content is visible then your tableview will not scroll

Resources