Hamza
Hamza

Reputation: 33

First UITableViewCell not calling didSelectRowAtIndexPath

The weirdest thing is happening when I select a UITableViewCell.

If I select the first one it doesn't do anything, but when I select the second one, It does call the didSelectRowAtIndexPath method, but with the values of the first one.

I should mention that I'm using a UIViewController with a UITableView inside of it. I should also mention that I just started using IOS 7.

Here is my code.

ChannelsPopoverViewController.h

@interface ChannelsPopoverViewController :UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *channelsTableView;

@end

ChannelsPopoverViewController.m

@implementation ChannelsPopoverViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
self.channelsTableView.delegate=self;
    self.channelsTableView.dataSource=self;
}


-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"new account cell"];
    UIImageView *channelImage=(UIImageView *)[cell viewWithTag:100];
    UILabel *cellLabel=(UILabel *)[cell viewWithTag:101];

    if (indexPath.row==0) {
        channelImage.image=[UIImage imageNamed:@"App-dropbox-icon.png"];
        cellLabel.text=@"add dropbox";
    }
    if (indexPath.row==1) {
        channelImage.image=[UIImage imageNamed:@"Google-Drive-icon.png"];
        cellLabel.text=@"add google drive";
    }



    return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=(UITableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    UILabel *cellLabel=(UILabel *)[cell viewWithTag:101];
    if ([cellLabel.text isEqual:@"add dropbox"]) {
        [[DBSession sharedSession] linkFromController:self];
    }

}

@end

Upvotes: 1

Views: 1640

Answers (3)

Oly Dungey
Oly Dungey

Reputation: 1677

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;
    }
}

Upvotes: 1

Nick
Nick

Reputation: 949

You are using didDeselectRowAtIndexPath, not didSelectRowAtIndexPath.

Upvotes: 3

damirstuhec
damirstuhec

Reputation: 6207

Fist of all:

[cellLabel.text isEqual:@"add dropbox"]

should really be

[cellLabel.text isEqualToString:@"add dropbox"]

Second of all:

You should really make use of indexPath in didSelectRowAtIndexPath method.

Example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 0)
    {
        [[DBSession sharedSession] linkFromController:self];
    }
    ...
}

Upvotes: 2

Related Questions