手动调用didSelectRowatIndexPath

问题描述 投票:23回答:5

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

[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];

给我以下错误:

使用未声明的标识符'indexPath';您是说'NSIndexPath'吗?

有人可以帮我吗?谢谢!

编辑:从下面的响应中,这听起来像是我以错误的方式进行操作。按下按钮时如何获取所选项目的文本(可以是多个选择)?我需要在专用于按钮按下的功能中执行此操作。

ios objective-c
5个回答
44
投票

您需要传递一个有效的参数,如果尚未在调用范围中声明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:内部完成的工作提取到单独的方法中,然后直接调用它们。

要解决更新的问题,您需要在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;
    }

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

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此时将包含所有选定的信息。希望该示例有意义。


30
投票

Swift 3.0解决方案

手动调用didSelectRowAtIndexPath

let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)

6
投票

仅更新@Sourabh's answer,请注意,在调用scrollPosition时也可以提供selectRow

let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)

成为:

let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .top) // <--
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)

possible constants是:

.none

表格视图将感兴趣的行滚动为完全可见,并带有运动最少。如果该行已经完全可见,则不会滚动发生。例如,如果该行在可见区域上方,则行为与top指定的行为相同。这是默认设置。

.top

表格视图将感兴趣的行滚动到可见行的顶部表格视图。

.middle

表格视图将感兴趣的行滚动到可见的表格视图。

.bottom

表格视图将感兴趣的行滚动到可见的表格视图。


2
投票

如果您还没有一个indexPath变量,请定义一个indexPath变量。

类似:

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

0
投票

Mistalis answer开始,我做了这个小ExtendedCell类,因为我在几个表视图中使用它。

在表视图中的用法:

// 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)
    }

    // ...
}

您的单元格的实现:

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类:

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)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.