ios - 通过快速单击按钮设置选定的行

标签 ios swift

我有一个表格 View 和一个按钮的单元格。 我想当我点击哪一行的按钮时,当前行被选中。 (我的意思是行按钮在里面)。

我写了下面的代码,但它只选择了第一行:

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

什么是解决方案

最佳答案

这里有几种可能性。 其中之一也是最简单的方法是使用标签。

要提供完整的解决方案,您首先需要在 cellForRowAtIndexPath 方法中向按钮添加标签。

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() 方法中的样子:

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

}

另一种可能性是使用闭包。

创建 UITableViewCell 的子类:

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 方法:

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

        // ...

}

注意:您也可以使用委托(delegate)。

它也可以工作:)

关于ios - 通过快速单击按钮设置选定的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265478/

相关文章:

ios - 根据选择器选择IOS确定Segue,swift

ios - 如何使可点击的 NSAttributedString 移动到另一个 ViewController Swift

ios - 在 swift 中动态加载类的实例

ios - 如何在 swift 中将值从可重用的 PickerView.xib 中传递出去

iphone - 从 iPad/iPhone 连接到 Airport Extreme 上的文件共享

ios - 等待直到NSURLConnection sendAsynchronousRequest完成

ios - XLPagerTabStrip 在 IOs Swift View Pager 中添加图像和标签

Swift 使用 ObjectMapper

swift - 如何在新 Controller 的tableView中打开数据,来自FIrebase的数据

objective-c - CLLocation 没有显示准​​确的结果