Joyce
Joyce

Reputation: 15

UITableVIew didSelectRowAtIndexPath inside VIewController not working

I added this code to my ViewController class under (#pragma mark - UITableViewDelegate Methods)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"selected cell = %@", cell.textLabel.text);

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

    NSLog(@"row = %ld", indexPath.row);
}

I just want to display the text that I select but it's not working. Any ideas what I'm doing wrong?

Full code is here: IOS 8 Objective-C Search bar and Table view Using Google Autocomplete

Thanks for the help!

EDIT: I also added the following code:

viewDidLoad method (ViewController.m)

_tableView.allowsSelectionDuringEditing = YES;
_tableView.delegate = self;
_tableView.dataSource = self;

changed ViewController.h code from

@interface ViewController : UIViewController

to

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Upvotes: 1

Views: 702

Answers (6)

maxweber
maxweber

Reputation: 576

Method name changed or wrong. You can drag-drop to set the ViewController as the delegate. But this method needed to process when row touched:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("section: \(indexPath.section)")
    print("row: \(indexPath.row)")
    // caller is set to the calling ViewController, when it "prepare" to launch this one
    caller.payFromSelection( selectedIndex: indexPath.row )
}

Back in the main controller, it gets the value and closes the child vc having the tableview...

func payFromSelection( selectedIndex:Int ) -> Void {
    print( selectedIndex )
    self.navigationController?.popViewController(animated: true)
    //self.navigationController?.popToRootViewController(animated: true)
}

Upvotes: 0

Pavel Malinnikov
Pavel Malinnikov

Reputation: 359

Adding the method is not enough. You should set your 'delegate' property of table view to the 'self' where 'self' means your view controller.

For example in your .m file:

@interface MyViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView *tableView;

@end


- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
}

...

#pragma mark - UITableViewDelegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
}

EDIT: Also please make sure you have connected the tableView property with a table view object inside of Interface Builder.

Upvotes: 0

Aura
Aura

Reputation: 246

Have you set this properties in Interface Builder?

Interface Builder

Upvotes: 0

Ricardo
Ricardo

Reputation: 2943

Use the debugger and break points. I would put a break point in:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

and see if the program stops there or not.

Upvotes: 0

Suhit Patil
Suhit Patil

Reputation: 12023

[tableView reloadRowsAtIndexPaths:]... only works when wrapped inbetween [tableView beginUpdates] and [tableView endUpdates];

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];

check if the delegate is set to self

_tableView.delegate = self;

Upvotes: 1

user3290180
user3290180

Reputation: 4410

in viewDidLoad set the delegates

_table.delegate = self;
_table.dataSource = self;

in the interface then declare the protocols

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Upvotes: 1

Related Questions