24

プログラムで didSelectRowAtIndexPath を呼び出そうとしていますが、問題が発生しています。

[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];

次のエラーが表示されます。

宣言されていない識別子 'indexPath' の使用。「NSIndexPath」のことですか?

誰でも私を助けることができますか?ありがとう!

編集:以下の回答から、私はこれについて間違った方法で進んでいるように聞こえます。ボタンが押されたときに選択されたアイテムのテキストを取得するにはどうすればよいですか (複数選択可能)? ボタンを押す専用の関数でこれを行う必要があります。

4

5 に答える 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更新された質問に対処するには、メソッド onを使用する必要があります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その時点で、選択したすべての情報が含まれます。うまくいけば、その例は理にかなっています。

于 2013-08-15T02:36:24.947 に答える
36

迅速な解決策

手動で didSelectRowAtIndexPath を呼び出す

let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
于 2016-11-14T13:37:06.560 に答える
2

そこに固執するものがまだない場合は、 indexPath 変数を定義する必要があります。

何かのようなもの:

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
于 2013-08-15T02:38:30.067 に答える
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)
        }
    }
}
于 2017-06-12T14:26:49.097 に答える