【问题标题】:Tell to a button which row of the tableView we are tapping告诉按钮我们正在点击 tableView 的哪一行
【发布时间】:2018-05-27 05:15:20
【问题描述】:

您好,在我的 tableView 中,我在我的自定义 tableViewCell 中添加了一个刷新按钮

因为我想在点击按钮后重新加载单个单元格,为此(环顾网络)我创建了这个函数

 @IBAction func rowReload(_ sender: Any) {

        let index = IndexPath(row: , section: )
        self.tableView.reloadRows(at: [index], with: .none)
    }

但我的问题是这一行let index = IndexPath(row: , section: ),我如何告诉我的函数重新加载操作必须发生在我按下按钮的行上? (简单来说,我接下来要写什么row:section:?)

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

如果UITableview中有多个section,那么我们不能使用标签来确定indexpath。

    let indexPath = tableView.indexPathForSelectedRow

【讨论】:

    【解决方案2】:

    在你的方法中,

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

    你可以给元素(按钮)标签号 例如。

    cell.button.tag = indexPath

    所以,当你点击按钮时,你可以得到标签作为它的 indexPath。

    @IBAction func rowReload(_ sender: Any) {

        let index = sender.tag
        self.tableView.reloadRows(at: [index], with: .none) 
    

    }

    【讨论】:

      【解决方案3】:

      在您的 cellForRow 方法中,将标签分配给如下所示的按钮:

      yourCell.yourButton.tag = indexPath.row
      

      然后在cellForRow方法中为它分配选择器。

      yourCell.yourButton.addTarget(self, action: #selector(myClass.rowReload(_:)), forControlEvents: .TouchUpInside)
      

      在您的 rowReload 方法参数中,sender 应该具有 UIButton 类型,您的方法将是:

      func rowReload(_ sender: UIButton) {
      

      然后您可以访问它的标签,您的IndexPath 将是:

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

      【讨论】:

      • 好的,但我必须添加@IBOutlet weak var refButton: UIButton!在我的控制器中还是在我的自定义 tableViewCell 类中?
      • 它应该在自定义tableViewCell中。
      • 那你可以这样:ioscake.com/…
      猜你喜欢
      • 2020-11-21
      • 2020-02-15
      • 1970-01-01
      • 2011-03-25
      • 2019-10-26
      • 2015-01-21
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多