0

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");
  }
}
Greg
  • 276
  • 1
  • 3
  • 25
  • Did you try debugging? Is it coming inside `didSelectRowAtIndexPath:` method ? – Deepesh Gairola May 01 '13 at 01:56
  • If didSelectRowAtIndexPath is not firing it's likely that you have not set your delegate for the tableview correctly. As for the double tap you would likely need to implement your own custom action method utilizing uigesturerecognizer to detect the taps. – ninehundreds May 01 '13 at 01:58
  • Do you still have [self.myTableView reloadData]; at the end of your updated didSelectRowAtIndexPath method? – rdelmar May 01 '13 at 02:16
  • @rdelmar i did. tried it both ways, but nothing worked. the tap is clearly working when i press the cell as i can see thru nslog, but for whatever reason the initial array just won't clear – Greg May 01 '13 at 02:18
  • 1
    Instead of NSLog(@"LOADED");, log NSLog(@"%@", ArrayNo) and see if it shows all the objects. – rdelmar May 01 '13 at 02:20
  • I think you're getting the wrong idea about your array. I'm not sure what you did, but you shouldn't have to removeAllObjects in order to see the new ones you added. You should try commenting out that line ([arrayNo removeAllObjects];) in the code you now have working and see if you see all your objects. There is no concept of "resetting" an array, and your code above was correctly adding the new objects (of course, if you want to remove the old objects first, that's fine, but you shouldn't have to in order to see the new ones). – rdelmar May 01 '13 at 03:09
  • hmm. not sure then. adding 'removeAllObjects' fixed the issue for me. maybe coincidence... – Greg May 01 '13 at 03:23

2 Answers2

3

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.

sosborn
  • 14,676
  • 2
  • 42
  • 46
0

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];
  }
}
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
  • Weird! This worked, but I can see how... I forgot about `removeAllObjects.` So, the items must have been added but because the array was called initially it wasn't ACTUALLY adding them? i.e. i had to "reset" it by using `removeAllObjects`? – Greg May 01 '13 at 02:24
  • @Thilina.Hewagama My only question now is how to make `didSelectRowAtIndexPath` select a cell after TWO touches, as oppose to one which it does by default... – Greg May 01 '13 at 02:29
  • 1
    This will help you mate, http://stackoverflow.com/questions/1031254/how-can-i-detect-a-double-tap-on-a-certain-cell-in-uitableview – Thilina Chamath Hewagama May 01 '13 at 02:35