gpt4 book ai didi

ios - indexPath.row 返回最后一个可见单元格,并且没有调用 didSelectRowAtIndexPath

转载 作者:行者123 更新时间:2023-11-28 19:37:31 25 4
gpt4 key购买 nike

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

- (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 是正确的。

最佳答案

cellForRowAtIndexPath 方法中删除以下行。

 _selectedRow = indexpath.row;

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

关于ios - indexPath.row 返回最后一个可见单元格,并且没有调用 didSelectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37398111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com