307

I'm writing an iOS app with a table view inside a tab view. In my UITableViewController, I implemented -tableView:didSelectRowAtIndexPath:, but when I select a row at runtime, the method isn't being called. The table view is being populated though, so I know that other tableView methods in my controller are being called.

Does anyone have any ideas what I may have screwed up to make this happen?

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Matt Pfefferle
  • 209
  • 2
  • 6
  • 14
  • 84
    also you may have a gestureRecognizer on top of the UITableView that absorbs the selection .. (one of the possibilities) – M.Othman Nov 10 '12 at 07:47
  • Be careful if the table view is being populate, it means the DataSource is well set. The selection is part of the Delegate methods. So maybe the DataSource is well set but not the Delegate ! – Thomas Besnehard Aug 08 '13 at 16:32
  • 1
    Hi M.Othman, your comment is exactly what was wrong with my own issue. Do you know how to get gestureRecognizer working 'with' the `UITableView`? – yhl Sep 30 '13 at 01:01
  • 15
    I notice that if I tap and hold, the tap eventually calls `-didSelectRowAtIndexPath`. I figured out hat's because I have a tap gesture recognizer on the tableview and the gesture has to fail first before receiving the touch. Though it's weird the table selection animation is not affected by the gesture recognizer. – Hlung Nov 22 '13 at 04:47
  • 6
    I had the same problem here and had the fortunate opportunity to learn that you cannot have a gestureRecognizer on top of the UITableView. Thanks M. Othman! – vnchopra Jan 02 '14 at 19:15

60 Answers60

535

Just in case someone made the same stupid mistake as I did:

Check out if the method name of what you expect of being didSelect may accidentally be gotten didDeselect in some way. It took about two hours for me to find out ...

CGee
  • 1,650
  • 5
  • 20
  • 31
  • 47
    Same issue here. It's because XCode autocompletes the Deselect before the Select. – jeremywhuff Jun 05 '13 at 17:09
  • Similarly, I had set allowsSelection = false, which as well as removing any selection highlight, also prevents the cell from being pressed! – djinne95 Oct 24 '19 at 13:31
520

Another thing that might lead to the issue is not selected selection kind:

UITableView selection kind

Should be Single Selection for normal selection, should not be No Selection.

To do this programmatically, do:

tableView.allowsSelection = YES
mfaani
  • 33,269
  • 19
  • 164
  • 293
Dennis Krut
  • 423
  • 1
  • 6
  • 11
  • 4
    I keep running into this because I have scenes that are always in edit mode and I keep forgetting to change the default from "No Selection During Editing". – Symmetric Dec 14 '12 at 22:50
  • 3
    You can add this line in your -viewDidLoad method of viewControlller NSParameterAssert(self.tableView.allowsSelection); – Nikolay Shubenkov Feb 16 '15 at 15:37
  • for those who observed tableView.rx.itemSelected.subscribe didnt call back, this is the issue and above fix works – Dhilip May 17 '18 at 13:52
  • so helpful when you take over some codebase and it has code using buttons with tags and not didSelectRow, with this selection disabled in the storyboard – Nitin Alabur Jan 30 '19 at 14:19
  • is this tableView default behaviour? – Frostmourne Nov 04 '19 at 08:24
305

Another possibility is that a UITapGestureRecognizer could be eating the events, as was the case here: https://stackoverflow.com/a/9248827/214070

I didn't suspect this cause, because the table cells would still highlight blue as if the taps were getting through.

Community
  • 1
  • 1
bugloaf
  • 2,890
  • 3
  • 30
  • 49
  • This was my problem! Thank you so much!! I mean it clearly worked before, but it stopped working after I implemented UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; – coolcool1994 Jul 04 '13 at 21:31
  • 29
    I had the same problem as you @coolcool1994 just implemented [tap setCancelsTouchesInView:NO]; and solved the issue. – nizx Jan 09 '14 at 04:42
  • Thank, it helps me. In my case `didSelectRowAtIndexPath` doesn't works only after delete cell in `commitEditingStyle`. Then I make `UITapGestureRecognizer` and in `tapAction:` got `indexPath` from `sender.view` (`[self.tableView indexPathForCell:sender.view]`) and call `[self.tableView.delegate didSelectRowAtIndexPath:myIndexPath]` – Alexmelyon Apr 18 '14 at 08:20
  • 1
    This was the my issue too .. add code to remove the tapgesture in tableview & it was work like charm - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init]; gesture.cancelsTouchesInView = NO; [self.tableView addGestureRecognizer:gesture]; } – Udaya Sri Aug 20 '14 at 07:24
  • 7
    Thanks @nizx, in swift 4 ***tap.cancelsTouchesInView = false*** – Ariven Nadar Apr 24 '18 at 11:14
151

All good answers, but there's one more to look out for...

(Particularly when creating a UITableView programmatically)

Make sure the tableView can respond to selection by setting [tableView setAllowsSelection:YES]; or removing any line that sets it to NO.

Old McStopher
  • 6,295
  • 10
  • 60
  • 89
  • 11
    I'd add `setAllowsSelectionDuringEditing:` to the list (when tableview is in editing mode) – alex-i Oct 29 '13 at 11:33
  • Thank you! In IB I had to change in the "Selection" section value to `Single Selection`. – Sasho Jul 31 '17 at 06:02
99

It sounds like perhaps the class is not the UITableViewDelegate for that table view, though UITableViewController is supposed to set that automatically.

Any chance you reset the delegate to some other class?

holex
  • 23,961
  • 7
  • 62
  • 76
Hunter
  • 4,343
  • 5
  • 42
  • 44
  • 7
    I discovered that while my controller was a UITableViewController, I used a plain old UIViewController widget in UI Builder. That doesn't work. When I deleted the UIViewController widget and dropped a UITableViewController in its place, everything worked. – Matt Pfefferle Nov 01 '08 at 20:51
  • 1
    Another boundary case for everyone - I wired up the delegate in code and had forgotten I had previously wired the delegate via the storyboard .... last one to set it wins. – Oly Dungey Aug 06 '14 at 12:34
  • 1
    can you show how to "reset a delegate to some other class" please? I have this same issue and can't manage to solve it – Alfro Jul 14 '16 at 08:19
  • 1
    myTableViewController.tableView.delegate = xxx – Hunter Jul 18 '16 at 00:12
95

If the problem arise with UITapGestureRecognizer you can fix this:

  • in Storyboard:

enter image description here

in code with Objective-C:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
[self.view addGestureRecognizer:tap];

[tap setCancelsTouchesInView:NO];

in code with Swift:

let tap = UITapGestureRecognizer(target: self, action:Selector("dismissKeyboard"))
view.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
68

I have encountered two things in this situations.

  1. You may have forgot to implement UITableViewDelegate protocol, or there's no delegation outlet between your class and your table view.

  2. You might have a UIView inside your row that is a first responder and takes your clicks away. Say a UIButton or something similar.

gilm
  • 7,690
  • 3
  • 41
  • 41
  • 5
    It started happening on iOS 7, disabling "User Interaction Enabled" of views within contentView fixed this. – Kof Oct 03 '13 at 12:18
  • 1
    @Kof nailed it. Creating a UITableViewCell in Xcode 5's interface builder creates it with user interaction enabled = YES. Bad Xcode 5! – jsd Oct 10 '13 at 00:12
46

I have had the same problem. And it was hard to find. But somewhere in my code was this:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

It must be return indexPath, else -tableView:didSelectRowAtIndexPath: is not being called.

dandan78
  • 13,328
  • 13
  • 64
  • 78
JackPearse
  • 2,922
  • 23
  • 31
  • 2
    for did SelectRow At IndexPath correct signature is: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { – coolcool1994 Jul 04 '13 at 21:23
  • For me, it wouldn't start calling tableView:didSelectRowAtIndexPath until I deleted willSelectRowAtIndexPath entirely – etayluz Jun 30 '15 at 16:13
40

If you added a gestureRecognizer on top of the UITableView, didSelectRowAtIndexPath will not get called.

So you need to use gestureRecognizer delegate method to avoid touch in particular view.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:YourTable]) {
        return NO;
    }
    return YES;
}
Vinu David Jose
  • 2,569
  • 1
  • 21
  • 38
30

I've ran into a problem where after months of not looking at my code I forgot that I implemented the following method due to some requirements which were not necessary

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath  *)indexPath{
    return NO;
}

It should be returning YES for a row in order to make it selected.

cpt.neverm1nd
  • 264
  • 5
  • 7
24

YOU MUST select these options

enter image description here

but if you want to make UITableViewnot highlighted on clicking then you should make changes in UITableViewCell properties.

Choose None option for Selection just like below

enter image description here

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Mohsin Qureshi
  • 1,203
  • 2
  • 16
  • 26
21

I had put a UITapGestureRecognizer on my table view to dismiss the keyboard which prevented didSelectRowAtIndexPath: from being called. Hope it helps someone.

Groot
  • 13,943
  • 6
  • 61
  • 72
19

I had the same problem,

The reason was using of UITapGestureRecognizer. I wanted the keyboard to dismiss when I tapped anywhere else. I realized that this overrides all tap actions, that is why, didSelectRowAtIndexPath function did not called.

When I comment the rows related with UITapGestureRecognizer, it works. Moreover you can check in the function of UITapGestureRecognizer selector if the tapped is UITableViewCell or not.

Jeyhun Karimov
  • 1,295
  • 19
  • 27
19

In case you have the same problem as me: Apparently, this method won't be called if your tableView is in edit mode. You have to set allowsSelectionDuringEditing to true.

Via this question: When editing, `UITableView` does not call didSelectRowAtIndexPath ??

Community
  • 1
  • 1
spiralstairs
  • 365
  • 4
  • 12
14

In my case, didSelctRowAtIndexPath not calling is due to that I have selected none in the Selection property of tableView, set to single selection solved my problem

enter image description here

Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
11

For Xcode 6.4, Swift 1.2 . The selection "tag" had been changed in IB. I don't know how and why. Setting it to "Single Selection" made my table view cells selectable again. enter image description here

MB_iOSDeveloper
  • 4,178
  • 4
  • 24
  • 36
10

Remember to set the datasource and delegate in the viewDidLoad method as follows:

[self.tableView setDelegate:self];

[self.tableView setDataSource:self];
Carlos
  • 1,319
  • 2
  • 17
  • 20
10

Even though another answer has been accepted, I'll add one more possible problem and solution for people who observe this issue:

If you have automatic reference counting (ARC) turned on, you may find that even after assigning your controller as a delegate of the view, the view's messages to the controller are not being received because ARC is deleting the controller. Apparently the UITableView's delegate pointer does not count as a reference for the ARC, so if that is the only reference to it, the controller will be dealloc'd. You can verify whether or not this is happening by implementing the dealloc method on the controller and setting a breakpoint or NSLog call there.

The solution is to keep track of the controller with a strong reference somewhere else, until you are sure you won't need it anymore.

Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
  • The controller being garbage-collected while its view is still displayed? Can this actually happen? – Drux Jul 29 '14 at 10:02
  • @Drux Yep! When this happened to me it was nearly the first version of Obj-C with garbage collection used on iOS and honestly they didn't do a great job. A lot of references were explicitly or implicitly "weak" that, IMO, should have been strong. Which led to oddities like garbage collection of objects in active use by the display layer. I haven't done iOS work in a while so I have no idea if this still happens on the latest version. – Andrew Gorcester Jul 29 '14 at 15:52
  • My code has growing memory size problem and I fixed somewhat of it and somewhere else in the code just after this didSelect wont get called the first time only. I don't know does this justify the problem or not. – Amber K May 07 '18 at 15:44
9

Giving my 2 cents on this.

I had a Custom UITableViewCell and there was a button covering the whole cell, so when the touch happened, the button was selected and not the cell.

Either remove the button or in my case, I set User Interation Enable to false on the button, that way the cell was the one selected.

gmogames
  • 2,993
  • 1
  • 28
  • 40
9

My problem was none of the above. And so lame. But I thought I would list it here in case it helps someone.

I have a tableViewController that is my "base" controller and then I create subclasses of this controller. I was writing all my code in the tableView:didSelectRowAtIndexPath routine in the "base" class. Completely forgetting that by default this routine had also been created (albeit with no code that did anything) in all of my subclasses as well. So when I ran my app, it ran the subclass version of the code, did nothing, and made me sad. So of course, once I removed the routine from the subclasses, it used mt "base" class routine and I'm in business.

I know. Don't laugh. But maybe this will save someone the hour I lost...

Jon
  • 2,932
  • 2
  • 23
  • 30
Steve
  • 111
  • 2
  • 7
8

If you read this, so still doesn't solve the problem.

I have custom cell, where checkbox "User Interaction Enabled" was disable. So, I Just switch on it. Good luck.

HotJard
  • 4,598
  • 2
  • 36
  • 36
7

I just had this and as has happened to me in the past it didn't work because I didn't pay attention to the autocomplete when trying to add the method and I actually end up implementing tableView:didDeselectRowAtIndexPath: instead of tableView:didSelectRowAtIndexPath:.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
7

Make sure you implemented tableView:didSelectRowAtIndexPath and not tableView:didDeSelectRowAtIndexPath

This is gotten me on more than a few occasions !!

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
SleepsOnNewspapers
  • 3,353
  • 3
  • 23
  • 29
6

Please check for UITapGestureRecognizer. In my case tapgesture was added for the view where tableview got placed, which is eating the user interaction of UITableview like didselect. After disabling tapgesture for the view, didselect delegate was triggered.

Prasad.Jakka
  • 159
  • 2
  • 8
5

If your table view is in editing mode (eg. [tableView setEditing:YES animated:NO];), you need to set tableView.allowsSelectionDuringEditing = YES;

yvetterowe
  • 1,239
  • 7
  • 20
  • 34
5

In my case the solution was to change NO to YES in the below function.

iOS 9+

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
turingtested
  • 6,356
  • 7
  • 32
  • 47
HonkyHonk
  • 86
  • 1
  • 3
  • This was my problem too. I have a multiple tableView implementation and forgot to separate the two tableViews in this method. – turingtested Oct 23 '17 at 13:54
5

Check if your viewController has following method:

- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

if you return "NO" didSelectRow won't be called

Mithra Singam
  • 1,905
  • 20
  • 26
4

Another mistake you could've done (as I did): if you set a segue at the cell, didSelectRowAtIndexPath is not called. You should set your segues at the view controller instead.

juanignaciosl
  • 3,435
  • 2
  • 28
  • 28
4

None of these answers worked for me. After about an hour, I figured out something very insidious:

I have a table view inside a cell of another table view. I decided to make an enclosing view that contains the inner table view, among other things. I called this view contentView and hooked it up in the xib.

Turns out that UITableViewCell already has a contentView and does weird things with it. The issue resolved itself when I renamed the property to mainContentView and reconnected the view to this renamed property.

xytor
  • 406
  • 4
  • 16
4

Take care about the UITableView properties in the storyboard, what happened in my case was that I had the combox in the storyboard selected as "Selection: Single Selection", that does not allow the method didSelectRowAtIndexPath run.

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Diego Maye
  • 327
  • 5
  • 11
4

In my case, I dynamically calculate the height of the TableView's SuperView at load time. Due to a miscalculation, the TableView was positioned outside of the SuperView. The TableView was drawn fine, however all interaction was disabled (and didSelectRowAtIndexPath was never called). Very hard to detect, since there is no visual indication that the TableView is not "accessible".

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
GK100
  • 3,664
  • 1
  • 26
  • 19
4

In my case the problem was I had a UITableViewCell subclass and I'd implemented these two methods: touchesBegan:withEvent: & touchesEnded:withEvent to handle a fancy animation on touch. But I'd forgotten to add the [super touchesBegan:touches withEvent:event]; method ad [super touchesEnded:touches withEvent:event]; to also inform the parent of cell of the touch.

So changing the code to following solved my problem:

-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    [super touchesBegan:touches withEvent:event];
    //blah blah blah
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    //rest of the code
}
0xKayvan
  • 368
  • 1
  • 4
  • 18
4

I know is old and the problem was resolved, but a had similar problem, I thought that the problem was with my custom UITableViewCell, but the solution was completely different - I restart XCode :) and then works ok ! almost like Windows :)

3

There are some amazing clues and answers in this post (maybe one of the best discussions I've seen!) The clues in here helped me figure out what my issue was, but I still spent many hours drooling on my keyboard trying to discover the issue, which was similar to other posts. However, how I finally discovered it was a bit different, so I wanted to share in case others come across it too.

Turns out my issue was that, in code, a superclass was adding a full-screen "error view" that was not hidden, but transparent. However, because it was over the tableview and "user action" was set to YES, it was intercepting my touches on the tableview.

I diagnosed this using Xcode's cool "Debug View Hierarchy" button. Here is a screen shot with annotations that hopefully explain what I did and how I ultimately diagnosed the issue.

In code, I simply had to do:

errorMessageView.setUserInteractionEnabled = NO;
// or
errorMessageView.hidden = YES;

Debug View Hierarchy

DustinB
  • 11,037
  • 5
  • 46
  • 54
  • My case was similar. A full screen "tap view" that was receiving tap events. Even removing the tap gesture recognizer did not solve it, because the "tap view" was higher in Z-order than my table view. I moved the "tap view" beneath the table view and the problem was resolved. – Mike Taverne May 02 '17 at 22:11
3

Ok, updating here as I just ran into this problem, and my issue was slightly different than found here.

I looked in IB and saw that my delegate WAS set, but it was set incorrectly to VIEW instead of File's Owner (right click on table view to see where delegate is pointing to).

Hope that helps someone

tbone
  • 15,107
  • 3
  • 33
  • 40
3
tableView?.allowsSelection = true

It's False by default in Xcode 11.7

Serg Smyk
  • 613
  • 5
  • 11
2

This was probably only in my case, but I had reloaded some files from a backup and things were not working, including this. After doing a full clean (Product > Clean or Shift + Command + K) it worked. Probably something got messed up in a precompiled header. Chances are that's not the problem for you, but it's worth a shot.

shim
  • 9,289
  • 12
  • 69
  • 108
2

If You have a custom cell, remember to set UserInteractionEnabled in the Xib (or via code) for the cell.

gdm
  • 7,647
  • 3
  • 41
  • 71
  • Please emphasis to set the value of `userInteractionEnabled` of the custom cell to **NO** – mr5 Mar 02 '16 at 09:08
2

Check if -heightForRowAtIndexPath doesnt return 0.

shyamsundar1988
  • 529
  • 4
  • 14
2

In my case, just one cell was having this problem. The cell included UITextView outlet in readonly mode. Though it was readonly before tap. Upon tapping, keyboard rose. It turned out that it still needed to disable the interaction.

cell.content.scrollEnabled = NO;
cell.content.editable = NO;
cell.content.userInteractionEnabled = NO;
cell.content.delegate = nil;

[cell.content resignFirstResponder];

Imju
  • 539
  • 5
  • 11
2

I was having this problem intermittently. Sometimes touching a cell would cause it to be selected. Sometimes it would not receive the touch event.

I am using a features introduced in ios8 called self-sizing cells. I came across this blog post that points out that:

When the table view is first displayed, you may find some of the cells are not sized properly. But when you scroll the table view, the new cells are displayed with correct row height. To workaround this issue, you can force a reload after the view appears:

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()
}

This fixed the issue for me. Even though the table view was rendering correctly, touch handling (specifically hitTest of UITableView) seemed to be subject to the above-mentioned bug.

benvolioT
  • 4,507
  • 2
  • 36
  • 30
2

I iterated all previous answers but none of them helped me. After some trial and error I found a different solution. I had a UIImageView covering the whole cell (as a background). By default a UIImageView has user interaction disabled. By enabling the imageviews user interaction delegate method -didSelectRowAtIndexPath: was called again. eg.

cell.imgView.userInteractionEnabled = YES;
2

If the cell you are clicking is being highlighted but the function is still not being called, double check your function's signature. It should look like this:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
John Doe
  • 397
  • 3
  • 18
1

Another possible cause seems to be that the UITableView is embed in a UIScrollView. I had this problem today because I inadvertently had a scroll view instead of a normal view as the root view of my controller.

Taum
  • 2,511
  • 18
  • 18
  • +1 That was my problem: Some of the flags on the `UIScrollView` were set incorrectly. Now works e.g. with these flags set to Yes (others to No) in IB: Shows Horizontal Indicator, Scrolling Enabled, Paging Enabled, Bounces, Bounces Zoom, Delays Content Touches, Cancellable Content Touches, User Interaction Enabled, Multiple Touch. – Drux Jul 29 '14 at 10:16
1

I had a call to cell.userInteractionEnabled buried in some customized table code.

Stupid, yet aren't most bugs?

HalR
  • 11,411
  • 5
  • 48
  • 80
1

Another crazy possibility: I was running my app on an iPhone 5S, and I used:

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

as I always did in the past.

However, I didn't look closely enough at my compiler warnings... it said I should change that above line to:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

64-bit compatibility! shakes fist

Be on the lookout in case you start pulling your hair out.

karlbecker_com
  • 969
  • 1
  • 10
  • 16
1

I've just had this issue, however it didn't occur straight away; after selecting a few cells they would stop calling didSelectItemAtIndexPath. I realised that the problem was the collection view had allowsMultipleSelection set to TRUE. Because cells were never getting deselected this stopped future calls from calling didSelectItemAtIndexPath.

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Mark Bridges
  • 8,228
  • 4
  • 50
  • 65
1

I had this problem myself. I had built the bones of the view in IB (just a View and a TableView) and the delegate wasn't set. I wired it up to File's Owner and it worked like a charm. ::save::

1

I have read all the answers and strongly agree with them. But it is entirely different in my case. I had new segue for my detailViewController linked directly to my tableCell in StoryBoard which caused this. So I had to remove that segue from my cell and linked it with the UITableViewController itself. Now by writing the following code it works,

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        // Do any operation
        performSegue(withIdentifier: "DetailSegue", sender: self)
    }

Hope this solution will help someone out there!

Sasi
  • 1,666
  • 2
  • 24
  • 44
0

I was having problem that control was not going in to didselect row after applying break point. problem was in view. I removed tab gesture from view. then its worked fine

0

I can't comment, write here. In my case didSelectRow worked, but not didDeselectRow. I set delegate and dataSource for tableView and this solved my case.

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Hot'n'Young
  • 491
  • 4
  • 12
0
  1. Add @interface ExampleViewController () <UITableViewDelegate, UITableViewDataSource>

  2. Make delegation in the storyboard

enter image description here

  1. Add code

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
       NSString *cellText = cell.textLabel.text;
    }
    
Bista
  • 7,869
  • 3
  • 27
  • 55
dip
  • 129
  • 1
  • 4
0

I'm not sure if anybody scrolls far enough to see this answer but since this is the most popular question about this subject and the answer wasn't there I'll add it:

As of Xcode 9 / Swift 4 all Objective-C methods should be marked @objc. The compiler does a reasonable job of recognizing where it should be applied however it doesn't figure out inheritance. For example:

class Delegate: NSObject, UITableViewDelegate {
}

class SuperDelegate: Delegate {
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}

This will not generate any warning, crashing or build error. However your line will not be called until you add @objc:

@objc class SuperDelegate: Delegate {
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}
Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
0

Make sure you call reloadData on the main thread. If you are calling that method from some kind of asynchronous method (eg: a network request of some sort), the table view may or may not respond properly (in some cases the app can even crash).

Use a main thread code block to perform the reloadData call, if the call is being made in some other method block (which you are unsure of):

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [tableView reloadData];
}];
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
0

in mine case i did a small mistake is I assigned:

tableView.isUserInteractionEnabled = false

it should be:

tableView.isUserInteractionEnabled = true
Paul.V
  • 386
  • 1
  • 3
  • 13
0

I just found another way to not get your didSelect method called. At some point, prolly during some error in func declaration itself, XCode suggested I add @nonobjc to my method :

@nonobjc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

This will continue to compile without complaint but you will never get called by the ui actions.

"That's my two cents and I'll be wanting my change back"

Dilapidus
  • 413
  • 5
  • 13
0

In my case, the problem was declaring the method as a private.

This didn't work:

private func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

This worked:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Mahmut Acar
  • 713
  • 2
  • 7
  • 26
0

you must check this selection must be single selection and editing must be no seleciton during edition and you change setting in uttableviewcell properties also you edit in table view cell style must be custom and identifier must be Rid and section is none

ATIQ UR REHMAN
  • 431
  • 3
  • 12
0

I had this issue, when the class name of my TableViewController had been set incorrectly in the Interface Builder.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
0

A common error is to write code that calls selectRowAtIndexPath or deselectRowAtIndexPath and assume that the call will trigger a call to your delegate's didSelectRowAtIndexPath. It does not.

The documentation for selectRowAtIndexPath and deselectRowAtIndexPath clearly states:

"These methods will not call the delegate methods tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:, nor will it send out a notification."

w0mbat
  • 2,430
  • 1
  • 15
  • 16