Greg
Greg

Reputation: 276

didSelectRowAtIndexPath not working?

Trying to determine why I can't get my new "Arts" menu to show after being selected in didSelectRowAtIndexPath. I realize it is not being used correctly... also, is there a way I can make it show after two taps, or does didSelectRowAtIndexPath by default only work on once press?

-(void)viewDidAppear:(BOOL)animated
{
 NSString *arts = @"Arts and Museums";

 [arrayNo addObject:arts];

 [[self myTableView] reloadData];
}

- (void)viewDidLoad
{
 [super viewDidLoad];
 arrayNo = [[NSMutableArray alloc] init];
 [[self myTableView] setDelegate:self];
 [[self myTableView] setDataSource:self];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return [arrayNo count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (!cell)
  {
    NSLog(@"CREATING NEW CELL");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0];
  }

  cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row];

  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
  {
    NSString *galleries = @"Art Galleries";
    NSString *dramatic = @"Dramatic Arts";
    NSString *museums = @"Museums";

    [arrayNo addObject:galleries];
    [arrayNo addObject:dramatic];
    [arrayNo addObject:museums];

   [self.myTableView reloadData];
  }
}

************ UPDATED *************

After updating my didSelectRowAtIndexPath with the code below, I see that it is referencing it, but my new menu items are not popping up to replace the old array. I tried using self.myTableView = nil and reloading the data but it would appear it has no effect?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
    {
      NSString *galleries = @"Art Galleries";
      NSString *dramatic = @"Dramatic Arts";
      NSString *museums = @"Museums";

      [arrayNo addObject:galleries];
      [arrayNo addObject:dramatic];
      [arrayNo addObject:museums];

      NSLog(@"LOADED");
  }
}

Upvotes: 0

Views: 1494

Answers (2)

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

For that you should change your code like this, (use [arrayNo removeAllObjects] to clear the array)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
    {
      NSString *galleries = @"Art Galleries";
      NSString *dramatic = @"Dramatic Arts";
      NSString *museums = @"Museums";

      [arrayNo removeAllObjects];

      [arrayNo addObject:galleries];
      [arrayNo addObject:dramatic];
      [arrayNo addObject:museums];

      NSLog(@"LOADED");
      [self.myTableView reloadData];
  }
}

Upvotes: 0

sosborn
sosborn

Reputation: 14694

Here is your problem:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

This is the pattern used for creating cells, but if you want to retrieve an existing cell, you should do this:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

Also, when you call this [self.myTableView reloadData]; it reloads all of you data so you might be wiping out whatever changes you made.

Upvotes: 3

Related Questions