ios 手动调用 didSelectRowatIndexPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18245441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually call didSelectRowatIndexPath
提问by john cs
I am trying to call didSelectRowAtIndexPath programmatically but am having trouble.
我试图以编程方式调用 didSelectRowAtIndexPath 但遇到了问题。
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
Gives me the following error:
给了我以下错误:
Use of undeclared identifier 'indexPath'; did you mean 'NSIndexPath'?
使用未声明的标识符“indexPath”;你的意思是'NSIndexPath'?
Can anybody help me out? Thanks!
有人可以帮我吗?谢谢!
EDIT: 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.
编辑:从下面的回复看来,我的做法是错误的。按下按钮时如何获取所选项目的文本(可以是多项选择)?我需要在专用于按钮按下的功能中执行此操作。
回答by Carl Veazey
You need to pass a valid argument, if you haven't declared indexPath
in the calling scope then you'll get that error. Try:
你需要传递一个有效的参数,如果你没有indexPath
在调用范围内声明,那么你会得到那个错误。尝试:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:ROW_YOU_WANT_TO_SELECT inSection:SECTION_YOU_WANT_TO_SELECT]
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
Where ROW_YOU_WANT...
are to be replaced with the row and section you wish to select.
ROW_YOU_WANT...
用您要选择的行和节替换何处。
However, you really shouldn't ever call this directly. Extract the work being done inside tableView:didSelectRowAtIndexPath:
into separate methods and call those directly.
但是,您真的不应该直接调用它。将内部正在完成的工作提取tableView:didSelectRowAtIndexPath:
到单独的方法中并直接调用它们。
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:
要解决更新的问题,您需要使用 上的indexPathsForSelectedRows
方法UITableView
。想象一下,您正在从字符串数组的数组中填充表格单元格文本,如下所示:
- (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
would at that point contain all selected information. Hopefully that example makes sense.
selectedTexts
此时将包含所有选定的信息。希望这个例子是有道理的。
回答by Sourabh Sharma
Swift 3.0 Solution
Swift 3.0 解决方案
Manually call didSelectRowAtIndexPath
手动调用 didSelectRowAtIndexPath
let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
回答by Mistalis
Just to update @Sourabh's answer, note that you can also provide a scrollPosition
when calling selectRow
:
只是为了更新@Sourabh 的回答,请注意,您还可以scrollPosition
在调用时提供selectRow
:
let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
Becomes:
变成:
let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .top) // <--
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
The possible constantsare:
在可能的常量是:
.none
The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by top. This is the default.
.top
The table view scrolls the row of interest to the top of the visible table view.
.middle
The table view scrolls the row of interest to the middle of the visible table view.
.bottom
The table view scrolls the row of interest to the bottom of the visible table view.
.none
表格视图滚动感兴趣的行,以最小的移动完全可见。如果该行已经完全可见,则不会发生滚动。例如,如果该行位于可见区域上方,则行为与 top 指定的行为相同。这是默认设置。
.top
表格视图将感兴趣的行滚动到可见表格视图的顶部。
.middle
表格视图将感兴趣的行滚动到可见表格视图的中间。
.bottom
表格视图将感兴趣的行滚动到可见表格视图的底部。
回答by Indi
You need to define an indexPath variable if you don't already have one to stick in there.
如果你还没有一个变量,你需要定义一个 indexPath 变量。
Something like:
就像是:
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
回答by Ferran Maylinch
Starting from Mistalis answer, I made this little ExtendedCell
class because I use it in several table views.
从Mistalis answer开始,我做了这个ExtendedCell
小类,因为我在几个表视图中使用它。
Usage in table view:
表视图中的用法:
// Here MyTableView is UITableView and UITableViewDelegate
// but you can separate them.
class MyTableView: UITableView, UITableViewDelegate {
override func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
return MyCell(identifier: identifier, table: self)
}
// ...
}
Implementation of your cell:
你的单元格的实现:
class MyCell : ExtendedCell {
convenience init(identifier: String?, table: MyTableView)
{
// in this example, MyTableView is both table and delegate
self.init(identifier: identifier, table: table, delegate: table)
// ...
}
// At any moment you may call selectTableRow() to select
// the row associated to this cell.
func viewItemDetail() {
selectTableRow()
}
}
ExtendedCell
class:
ExtendedCell
班级:
import UIKit
/**
* UITableViewCell with extended functionality like
* the ability to trigger a selected row on the table
*/
class ExtendedCell : UITableViewCell {
// We store the table and delegate to trigger the selected cell
// See: https://stackoverflow.com/q/18245441/manually-call-did-select-row-at-index-path
weak var table : UITableView!
weak var delegate : UITableViewDelegate!
convenience init(identifier: String?, table: UITableView, delegate: UITableViewDelegate)
{
self.init(style: .default, reuseIdentifier: identifier)
self.table = table
self.delegate = delegate
}
func selectTableRow()
{
if let indexPath = table.indexPath(for: self) {
table.selectRow(at: indexPath, animated: true, scrollPosition: .none)
delegate.tableView!(table, didSelectRowAt: indexPath)
}
}
}