手动调用didSelectRowatIndexPath [英] Manually call didSelectRowatIndexPath
问题描述
我试图以编程方式调用didSelectRowAtIndexPath,但遇到了麻烦。
I am trying to call didSelectRowAtIndexPath programmatically but am having trouble.
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
给我以下错误:
使用未声明的标识符'indexPath';你是说'NSIndexPath'吗?
Use of undeclared identifier 'indexPath'; did you mean 'NSIndexPath'?
任何人都可以帮帮我吗?谢谢!
Can anybody help me out? Thanks!
编辑:从下面的回复中,听起来我正在以错误的方式进行此操作。按下按钮时,如何获取所选项目的文本(可以是多个选项)?我需要在专用于按下按钮的功能中执行此操作。
From the responses below it sounds like im going about this the wrong way. How can I get the text of the selected items when a button is pressed (can be multiple selections)? I need to do this in a function dedicated to the button press.
推荐答案
如果你避风港,你需要传递一个有效的参数在调用范围内声明 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.
这篇关于手动调用didSelectRowatIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!