1

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>
Community
  • 1
  • 1
Joyce
  • 15
  • 7

6 Answers6

1

in viewDidLoad set the delegates

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

in the interface then declare the protocols

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
user3290180
  • 4,260
  • 9
  • 42
  • 77
  • You can also se the the delegates by ctrl-dragging from table view to view controller on storyboard. – Blackus Jun 15 '15 at 07:49
  • I forgot to mention I did this already. I'm sorry for that >.< But, I think I'm still missing something because it's still not showing after I did this >. – Joyce Jun 15 '15 at 07:49
1

[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;
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

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.

Ricardo
  • 2,831
  • 4
  • 29
  • 42
  • Thanks for this! It doesn't seem to stop there. I tried to NSLog random text just to see if it entered the didSelectRowAtIndexPath method during selection, but it didn't output anything. – Joyce Jun 15 '15 at 07:53
  • In that case you know the problem is with connections, that is, delegates. Specifically UITableViewDelegate (not UITableViewDataSource) – Ricardo Jun 15 '15 at 07:56
  • That does seem to be the problem. The UITableViewDataSource Methods work just fine, but it won't enter any of the UITableViewDelegate Methods – Joyce Jun 15 '15 at 08:14
  • Nice, if you mark my response as correct, it would be even better :) BTW I suggest you use interface builder to make these connections from the view (tableview) to your controller (tableviewcontroller). – Ricardo Jun 15 '15 at 09:10
  • But the problem's not fixed yet? – Joyce Jun 15 '15 at 10:10
0

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.

Pavel Malinnikov
  • 349
  • 5
  • 11
0

Have you set this properties in Interface Builder?

Interface Builder

Aura
  • 246
  • 1
  • 10
0

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)
}
maxweber
  • 576
  • 1
  • 5
  • 12