0

试图确定为什么我的新“艺术”菜单在被选中后无法显示,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");
  }
}
4

2 回答 2

3

这是你的问题:

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

这是用于创建单元格的模式,但如果您想检索现有单元格,您应该这样做:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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

于 2013-05-01T01:56:51.480 回答
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];
  }
}
于 2013-05-01T02:19:14.790 回答