didSelectRowAtIndexPath不工作?

时间:2013-05-01 01:50:30

标签: ios objective-c uitableview

试图确定为什么我在didSelectRowAtIndexPath.中被选中后无法显示新的“艺术”菜单我发现它没有被正确使用...也有,我有办法让它成功两次点击后显示,或者didSelectRowAtIndexPath默认情况下只能按一次?

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

** * ** * ** * 的*** 更新 ** * ** * ** * ****

使用下面的代码更新我的didSelectRowAtIndexPath后,我发现它正在引用它,但是我的新菜单项没有弹出来替换旧的数组。我尝试使用self.myTableView = nil并重新加载数据,但它似乎没有效果?

- (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");
  }
}

2 个答案:

答案 0 :(得分:3)

这是你的问题:

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

这是用于创建单元格的模式,但如果要检索现有单元格,则应执行以下操作:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

此外,当您调用此[self.myTableView reloadData];时,它会重新加载所有数据,因此您可能会删除所做的任何更改。

答案 1 :(得分:0)

为此您应该像这样更改代码,(使用[arrayNo removeAllObjects]清除数组)

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