手动调用didSelectRowatIndexPath

时间:2013-08-15 02:32:51

标签: ios objective-c

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

[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];

给我以下错误:

  

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

有人可以帮帮我吗?谢谢!

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

5 个答案:

答案 0 :(得分:43)

你需要传递一个有效的参数,如果你没有在调用范围中声明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将包含所有选定的信息。希望这个例子有意义。

答案 1 :(得分:24)

Swift 3.0解决方案

手动调用didSelectRowAtIndexPath

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

答案 2 :(得分: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

     

表视图使用a滚动感兴趣的行以使其完全可见   运动最少。如果该行已完全可见,则不滚动   发生。例如,如果行位于可见区域上方,则   行为与top指定的行为相同。这是默认值。

     

top

     

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

     

middle

     

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

     

bottom

     

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

答案 3 :(得分:2)

如果你还没有一个indexPath变量,你需要定义它。

类似的东西:

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

答案 4 :(得分: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)
        }
    }
}