【问题标题】:indexPath.row returns last visible cell, and didSelectRowAtIndexPath not being calledindexPath.row 返回最后一个可见单元格,并且没有调用 didSelectRowAtIndexPath
【发布时间】:2023-04-05 23:26:02
【问题描述】:

我有一个 NSArray 和一个带有一些按钮的 tableView,它们的标题是当前 indexpath 行的数组中的字符串

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath {

 _selectedRow = indexpath.row;
 static NSString *simpleTableIdentifier = @"customCell";
 cell = [myTableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

 if (cell == nil) {
  cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
 }

 [cell.playButton setTitle:array[indexpath.row] forState: UIControlStateNormal];

 return cell;

}

按钮标题显示得很好。

现在我有一些mp3文件,名字和数组中的字符串一样,我想播放所选单元格对应的文件。

fileToPlay = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", array[_selectedRow]]; ofType:@"mp3"];

这里发生的情况是,播放的文件始终是对应于表格中最后一个可见单元格的文件。

我也有

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexpath {
 _selectedRow = indexpath.row;
}

但如果我尝试在此处打印 _selectedRow,则日志中不会出现任何内容。

当我点击表格中的一个单元格时,它似乎没有被选中(它不是灰色的)。

dataSource 和 delegate 也很好地连接到 tableview。

更新

我发现如果我点击按钮,就好像我没有点击选定的行。如果我单击单元格(按钮外),则 indexPath.row 是正确的。

【问题讨论】:

  • 试试这个:将 myGestureRecognizer.cancelsTouchInView 设置为 false
  • 为什么要添加 _selectedRow = indexpath.row;在 cellForRowAtIndex 中?
  • 您确定didSelectRowAtIndexPath 在您选择单元格时会被调用吗?它可能根本没有被选中,这将使您的 _selectedRow 作为最后一个加载的单元格(如果您向下滚动,则为底部,如果您向上滚动,则为顶部...)。
  • 更好的选择你可以在按钮单击 [cell.playButton setTag:indexPath.row] 上使用这样的方法; [cell.playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
  • 我发现如果我点击按钮,就好像我没有点击选定的行。如果我单击单元格(按钮外),则 indexPath.row 是正确的。

标签: ios objective-c uitableview tableviewcell didselectrowatindexpath


【解决方案1】:

cellForRowAtIndexPath 方法中删除以下行。

 _selectedRow = indexpath.row;

每次调用cellForRowAtIndexPath 时,您都在设置_selectedRow 值,即绘制每个单元格并解释为什么要使用最后一个单元格的值。

【讨论】:

  • 是的,我只是在尝试。但这里的问题是,当我点击单元格中的按钮时,我实际上并没有点击单元格,所以 selectedRow 没有更新。如果我点击单元格(按钮外),它就是。
  • 你真的需要一个按钮吗?似乎按钮正在消耗触摸事件。你可以尝试设置 button.userInteractionEnabled=NO;在单元格内的按钮上?
  • 我需要它来播放音频
  • 不能用 didSelectRowAtIndexPath 方法播放音频吗?
  • 是的,我只是添加 _selectedRow = indexPath.row; [自听科罗];到 didSelectRowAtIndexPath 。谢谢!
【解决方案2】:

设置代码:

 - (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath {
 cell.playButton.tag = indexpath.row;
[cell.playButton addTarget:self action:@selector(btnPlay:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)btnPlay:(id)sender{
    UIButton *btn = (UIButton *)sender;
    _selectedRow = btn.tag;
}

您正在选择触摸颜色集的选择颜色。

【讨论】:

    【解决方案3】:

    试试这些:

    1) 将 myGestureRecognizer.cancelsTouchInView 设置为 false...也许您的触摸会妨碍您。 (当您可能有手势识别器时,这是一个常见问题)

    2) 在 tableView 的属性检查器中,将 Selection 设置为单例。这解决了我的问题

    3) 设置:[tableView setAllowsSelection:YES];

    【讨论】:

    • 好的,这样做:在 didselectrowatindexpath 处设置断点。检查方法是否被调用
    • 问题是,如果没有调用 didselectrowatindexpath,那么 _selectedrow 的值不会改变,因此您选择的不是它们突出显示的内容
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多