-3

I have a UITableView which when you select a row, it will lead you into the "details" of the selected row. However, the first time I click the row, it goes into the prepareForSegue method, without calling didSelectRowAtIndexPath. When I push the back button on the detail view, the app goes back, and if I select the row again, the method will be called.

I'm NOT using the didDeselectRowAtIndexPath.

This patter will continue and I can't figure out why. When I load my data, I do call [tableview reloadData].

I have also tried to add self.TagsTableView.allowsMultipleSelection = YES; to my viewDidLoad method.

Here is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bluetoothManager = [[BlueToothLEManager alloc] init];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView:) name:@"ScanComplete" object:nil];


    self.Tags = self.bluetoothManager.Tags; 
    NSLog(@"Fetch Tags: %@", self.Tags); 
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell* cell = [fetchTagsTableView dequeueReusableCellWithIdentifier:@"tagCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"tagCell"];
    }

    FetchTag* newTag = [fetchTags objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %d", newTag.tagName, indexPath.row];
    cell.detailTextLabel.text = [newTag.tagUUID UUIDString];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedTagFromTable =  [fetchTags objectAtIndex:indexPath.row];
    [TagsTableView deselectRowAtIndexPath:indexPath animated:YES];
}
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    if([[segue identifier] isEqualToString:@"DetailView"]){
        TagDetailViewController* ftdvc = [segue destinationViewController];
        [ftdvc setSelectedTag:selectedTagFromTable];


    }
    // Pass the selected object to the new view controller.
}

-(void)reloadTableView:(NSNotification*)notif{
    NSLog(@"Reloading Table View");
    NSLog(@"Fetch Tags: %@", self.Tags); 
    [self.TagsTableView reloadData];
}
JaredH
  • 2,338
  • 1
  • 30
  • 40
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • 1
    So you are expecting to use a storyboard segue to navigate automatically on selection _and_ call `didSelectRowAtIndexPath`? I'm not sure I would trust that. I suggest deleting the automatic segue, and instead calling the segue manually (or just doing the navigation manually) _in_ `didSelectRowAtIndexPath`. – matt Dec 24 '14 at 18:04
  • And are you even using custom cells? Are you pre-populating the "fetchTagsTableView" cells (i.e. UITableViewCell* cell = [fetchTagsTableView dequeueReusableCellWithIdentifier:@"tagCell"];) somewhere else in your code? – Lyndsey Scott Dec 24 '14 at 18:07
  • Related: http://stackoverflow.com/q/18165684/335858 – Sergey Kalinichenko Dec 24 '14 at 18:10
  • @matt I'm using `didSelectRowAtIndex` to populate an object with data, the using `prepareSegue` to send the data to the new view controller. Is this not the right way to do this? Sorry, I'm new to iOS. I come from a Java/Android background. I'm still trying to figure this stuff out. – BlackHatSamurai Dec 24 '14 at 18:25
  • @BlackHatSamurai I already told you what I think is the "right way"; I don't see what you're asking me except to repeat myself. – matt Dec 24 '14 at 18:27
  • @matt I'm asking you HOW you would do that. – BlackHatSamurai Dec 24 '14 at 21:15
  • @BlackHatSamurai Well, that would be a new question! What is it that you don't know? How to run a segue in code? How to push a view controller in code? Do you know what a view controller _is_? Are you in a navigation controller interface? Do you know what a navigation controller is? How far back do you need me to go? This section of my book describes (and gives code for) what I usually do when the user taps on a row of my table: http://www.apeth.com/iOSBook/ch21.html#_table_view_selection – matt Dec 24 '14 at 21:56
  • @matt, thanks but I figured it out. – BlackHatSamurai Dec 24 '14 at 21:59

1 Answers1

0

I would have put the code from the didSelectRowAtIndexPath: to the prepareForSegue: instead Try using the sender parameter to identify a selected tag