2017-09-17 17 views
0

私はテーブルビューと1つのボタン付きセルを持っています。 どの行のボタンをクリックすると、現在の行が選択されていますか。 (私は行ボタンがそれにあることを意味する)。ボタンをクリックして選択行を設定します

私はコードの下に書きますが、それだけで最初の行を選択します。

@IBAction func btnShowAds(_ sender: Any) { 

    let indexPath = IndexPath(row: 0, section: 0) 
    tblMain.selectRow(at: indexPath, animated: true, scrollPosition: .bottom) 
    tblMain.delegate?.tableView!(tblMain, didSelectRowAt: indexPath) 
} 

ソリューション

+0

可能な複製([私はcell.swiftでindexPath.rowを取得できますか] https://stackoverflow.com/questions/40437550/how-can-i- get-indexpath-row-in-cell-swift) – matiastofteby

答えて

0

あなたがここにいくつかの可能性を持っては何です。 それらのうちの1つは、タグを使用するのが最も簡単です。

完全なソリューションを提供するには、まずcellForRowAtIndexPathメソッドでボタンにタグを追加する必要があります。

func handleButtonTapped(sender: UIButton) { 

    // Now you can easily access the sender's tag, (which is equal to the indexPath.row of the tapped button). 

    // Access the selected cell's index path using the sender's tag like so : 

    let selectedIndex = IndexPath(row: sender.tag, section: 0) 

    // And finally do whatever you need using this index : 

    tableView.selectRow(at: selectedIndex, animated: true, scrollPosition: .none) 

    // Now if you need to access the selected cell instead of just the index path, you could easily do so by using the table view's cellForRow method 

    let selectedCell = tableView.cellForRow(at: selectedIndex) as! YourCustomCell 

} 

もう一つの可能​​性、クロージャを使用することになります

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: yourReuseIdentifier, for: indexPath) as! YourCustomCell 

    // Set your button tag to be equal to the indexPath.row: 

    cell.button.tag = indexPath.row 

    // Add a target to your button making sure that you return the sender like so: 

    cell.button.addTarget(self, action: #selector(handleButtonTapped(sender:)), for: .touchUpInside) 
} 

そして今、これはあなたのhandlerButtonTapped()メソッド内のように、それがどのように見えるかです。

のUITableViewCellのサブクラスを作成します。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     // ... 

     cell.shouldSelectRow = { (selectedCell) in 

      // Since you now know which cell got selected by the user, you can access via its index path: 

      let selectedIndex = self.tableView.indexPath(for: selectedCell) 

      // Do whatever you need using the selected cell here 

      self.tableView.selectRow(at: selectedIndex, animated: true, scrollPosition: .none) 
     } 

     // ... 

} 

注:あなたはまた、デリゲートを使用することができ

class CustomTableCell: UITableViewCell { 

    var shouldSelectRow: ((CustomTableCell) -> Void)? 

    // MARK: User Interaction 

    @IBAction func handleDidTapButton(_ sender: UIButton) { 

     // Call your closure whenever the user taps on the button: 

     shouldSelectRow?(self) 
    } 
} 

今、あなたは、このようなあなたのcellForRowAtIndexPath方法を設定することができます。

そして、それは同様に動作します:)

+0

タグを使用して、テーブルビューのボタンのインデックスパスを追跡しないでください。テーブルビューで任意の行を挿入、削除、または移動できる場合は失敗します。 – rmaddy

+0

さて、OPは彼がセルの挿入/削除を実装するかどうかは言わなかったので、私は彼に彼のニーズに最適なものを選ぶことができるようにいくつかの異なる解決策を与えるだろうと思った。 – Alex

+1

それは問題ありません。私は、解決策を探している将来の読者にとって潜在的な問題を指摘しています。 – rmaddy

関連する問題