【问题标题】:didSelectRowAtIndexPath not working?didSelectRowAtIndexPath 不起作用?
【发布时间】:2013-05-01 01:50:30
【问题描述】:

试图确定为什么我在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");
  }
}

【问题讨论】:

  • 您是否尝试过调试?是否进入didSelectRowAtIndexPath: 方法?
  • 如果 didSelectRowAtIndexPath 没有触发,很可能是您没有正确设置 tableview 的委托。至于双击,您可能需要使用 uigesturerecognizer 来实现自己的自定义操作方法来检测点击。
  • 你还有没有[self.myTableView reloadData];在更新的 didSelectRowAtIndexPath 方法结束时?
  • @rdelmar 我做到了。两种方式都试过了,但没有任何效果。当我按下单元格时,水龙头显然在工作,因为我可以通过 nslog 看到,但无论出于何种原因,初始数组都不会清除
  • 代替 NSLog(@"LOADED");,记录 NSLog(@"%@", ArrayNo) 看看是否显示所有对象。

标签: ios objective-c uitableview


【解决方案1】:

这是你的问题:

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

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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

【讨论】:

    【解决方案2】:

    为此,您应该像这样更改代码,(使用 [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];
      }
    }
    

    【讨论】:

    • 奇怪!这行得通,但我可以看到...我忘记了removeAllObjects. 所以,这些项目必须已经添加,但是因为最初调用了数组,它实际上并没有添加它们?即我必须使用removeAllObjects“重置”它?
    • @Thilina.Hewagama 我现在唯一的问题是如何让didSelectRowAtIndexPath 在两次触摸后选择一个单元格,而不是默认情况下...
    • 这将帮助你交配,stackoverflow.com/questions/1031254/…
    • @Thilina.Hewagama 感谢您的帮助
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 2012-08-22
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多