且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

手动调用didSelectRowatIndexPath

更新时间:2023-01-23 19:04:17

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

You need to pass a valid argument, if you haven't declared indexPath in the calling scope then you'll get that error. Try:

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

其中 ROW_YOU_WANT ... 将是替换为您要选择的行和部分。

Where ROW_YOU_WANT... are to be replaced with the row and section you wish to select.

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

However, you really shouldn't ever call this directly. Extract the work being done inside tableView:didSelectRowAtIndexPath: into separate methods and call those directly.

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

To address the updated question, you need to use the indexPathsForSelectedRows method on UITableView. Imagine you were populating the table cell text from an array of arrays of strings, something like this:

- (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;
    }

然后,为了获得所有选定的文字,你想做点什么喜欢:

Then, to get all the selected text, you'd want to do something like:

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 会在该点包含所有选定的信息。希望这个例子有意义。

selectedTexts would at that point contain all selected information. Hopefully that example makes sense.