ios - 手动调用 didSelectRowatIndexPath

标签 ios objective-c

我正在尝试以编程方式调用 didSelectRowAtIndexPath,但遇到了问题。

[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];

给我以下错误:

Use of undeclared identifier 'indexPath'; did you mean 'NSIndexPath'?

有人能帮帮我吗?谢谢!

编辑:从下面的回复来看,我似乎以错误的方式处理这件事。如何在按下按钮时获取所选项目的文本(可以多选)?我需要在专用于按钮按下的功能中执行此操作。

最佳答案

你需要传递一个有效的参数,如果你没有在调用范围内声明 indexPath 那么你会得到那个错误。尝试:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:ROW_YOU_WANT_TO_SELECT inSection:SECTION_YOU_WANT_TO_SELECT]
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];

ROW_YOU_WANT... 将替换为您希望选择的行和部分。

但是,您真的不应该直接调用它。将 tableView:didSelectRowAtIndexPath: 中完成的工作提取到单独的方法中并直接调用它们。

要解决更新后的问题,您需要在 UITableView 上使用 indexPathsForSelectedRows 方法。想象一下,您正在从一组字符串数组中填充表格单元格文本,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tv dequeue...];
        NSArray *rowsForSection = self.sectionsArray[indexPath.section];
        NSString *textForRow = rowsForSection[indexPath.row];
        cell.textLabel.text = textForRow;
        return cell;
    }

然后,要获取所有选定的文本,您需要执行以下操作:

NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
NSMutableArray *selectedTexts = [NSMutableArray array];
for (NSIndexPath *indexPath in selectedIndexPaths) {
    NSArray *section = self.sectionsArray[indexPath.section];
    NSString *text = section[indexPath.row];
    [selectedTexts addObject:text];
}

selectedTexts 此时将包含所有选定的信息。希望这个例子有意义。

关于ios - 手动调用 didSelectRowatIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18245441/

相关文章:

objective-c - 如何创建可变函数? (可以接受任意数量的参数)

ios - (_hidden#919_ :0) inside crash symbolication file

objective-c - 如何使用 keywindow 无边框窗口(覆盖)覆盖全屏游戏?

objective-c - 重新定位 MKMapView 的 userLocationView?

ios - SpriteKit SKPhysicsBody bodyWithTexture 颠倒了

iphone - 调用协议(protocol)方法是否通过程序流控制?

objective-c - 架构 i386 的 undefined symbol : "_SCNetworkReachabilitySetCallback"

objective-c - 如何正确使用 NSTimeInterval?

iphone - 重复符号_OBJC_METACLASS_$_SBJsonParser

ios - 如何从页面 View Controller 获取当前 View