Ios 如何仅在选定单元格中启用按钮?

Ios 如何仅在选定单元格中启用按钮?,ios,swift,tableview,Ios,Swift,Tableview,我有一个内置TableView的ViewController。TableView是动态的,其中有一个带有按钮的自定义单元格。单元格的数量将取决于api 因此,每个单元格都保存一些从api传递的信息,每个单元格都有一个INFO按钮。 默认情况下,我设置 Btninfo.isUserInteractionEnabled = false 当用户点击一个单元格时 cell?.Btninfo.isUserInteractionEnabled = true 通过这种方法,在用户选择单元格之前不会选择按钮

我有一个内置TableView的ViewController。TableView是动态的,其中有一个带有按钮的自定义单元格。单元格的数量将取决于api

因此,每个单元格都保存一些从api传递的信息,每个单元格都有一个INFO按钮。 默认情况下,我设置

Btninfo.isUserInteractionEnabled = false
当用户点击一个单元格时

cell?.Btninfo.isUserInteractionEnabled = true
通过这种方法,在用户选择单元格之前不会选择按钮,一旦用户选择了单元格,就会从单元格中获取信息,并使用单元格的indexPath.row获取一些其他详细信息。 这个信息按钮会打开一个弹出窗口,显示细胞内物质的详细信息

现在的问题是:

一旦用户选择任何单元格,Btninfo将被启用,并将从该单元格获取信息。现在,如果用户单击另一个单元格中的Infobutton而未选择该单元格,则弹出窗口将显示从上一个单元格获取的详细信息

那么,如何仅为所选单元格启用按钮,并对其他单元格保持禁用状态?然后禁用该单元格中的按钮,并在下一个单元格用户选择时启用它


(我知道我可以禁用DIDDENSELECT上的单元格,我不会问这个问题)

我现在正在代码中使用类似的东西

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! Cell
    cell.Btninfo.isUserInteractionEnabled = cell.isSelected
}  

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? Cell {
        cell.Btninfo.isUserInteractionEnabled = true
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? Cell {
        cell.Btninfo.isUserInteractionEnabled = false
    }
}

我现在正在代码中使用类似的东西

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! Cell
    cell.Btninfo.isUserInteractionEnabled = cell.isSelected
}  

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? Cell {
        cell.Btninfo.isUserInteractionEnabled = true
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? Cell {
        cell.Btninfo.isUserInteractionEnabled = false
    }
}

您使用了一个模型类数组,如
arrData:[Data]=[]
在表视图中显示数据

请在模型类中添加额外的变量,如
isAbleToEnable=false
,您将使用该变量来显示数据。 模型类将类似于:

Class Data {
  ….
  {Your Content}
  ….
  var isAbleToEnable = false
}
现在在tableview方法中,执行以下操作

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     self.arrData[indexPath.row].isAbleToEnable = true
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! Cell
    If  self.arrData[indexPath.row].isAbleToEnable {
       cell.Btninfo.isUserInteractionEnabled = true
    //do stuff
   } else {
       cell.Btninfo.isUserInteractionEnabled = false
   }
}

您使用了一个模型类数组,如
arrData:[Data]=[]
在表视图中显示数据

请在模型类中添加额外的变量,如
isAbleToEnable=false
,您将使用该变量来显示数据。 模型类将类似于:

Class Data {
  ….
  {Your Content}
  ….
  var isAbleToEnable = false
}
现在在tableview方法中,执行以下操作

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     self.arrData[indexPath.row].isAbleToEnable = true
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! Cell
    If  self.arrData[indexPath.row].isAbleToEnable {
       cell.Btninfo.isUserInteractionEnabled = true
    //do stuff
   } else {
       cell.Btninfo.isUserInteractionEnabled = false
   }
}

在自定义
UITableViewCell
自身中处理该问题

  • 要基于单元格选择启用/禁用
    Btninfo的
    用户交互
    ,请在
    UITableViewCell
    中覆盖
    setSelected(:,animated:)
    方法

  • 要禁用
    Btninfo的
    userInteraction,当重新使用单元格时,请在
    prepareforeuse()
    方法中禁用它

  • 我想说的是

    class CustomCell: UITableViewCell {
        @IBOutlet weak var Btninfo: UIButton!
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            self.Btninfo.isUserInteractionEnabled = selected
        }
    
        override func prepareForReuse() {
            super.prepareForReuse()
            self.Btninfo.isUserInteractionEnabled = false
        }
    }
    

    setSelected(:,animated:)
    方法将自动处理所有
    单元格中的
    Btninfo
    isUserInteractionEnabled
    ,即在所选的
    单元格中,它将启用它,在其他
    单元格中,它将禁用它。您不需要在
    tableView(\uDidSelectRowat:)
    方法中执行任何操作。

    在自定义
    UITableViewCell
    本身中处理该操作

  • 要基于单元格选择启用/禁用
    Btninfo的
    用户交互
    ,请在
    UITableViewCell
    中覆盖
    setSelected(:,animated:)
    方法

  • 要禁用
    Btninfo的
    userInteraction,当重新使用单元格时,请在
    prepareforeuse()
    方法中禁用它

  • 我想说的是

    class CustomCell: UITableViewCell {
        @IBOutlet weak var Btninfo: UIButton!
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
            self.Btninfo.isUserInteractionEnabled = selected
        }
    
        override func prepareForReuse() {
            super.prepareForReuse()
            self.Btninfo.isUserInteractionEnabled = false
        }
    }
    

    setSelected(:,animated:)
    方法将自动处理所有
    单元格中的
    Btninfo
    isUserInteractionEnabled
    ,即在所选的
    单元格中,它将启用它,在其他
    单元格中,它将禁用它。您不需要在
    tableView(u:didSelectRowAt:)
    方法中执行任何操作。

    您可以启用所有单元格信息按钮,并在视图中启用一个目标控制器,该控制器可以使用正确的信息弹出显示,因为不需要两次点击。根据所选单元格的indexPath,我编写了有关从数组获取数据的所有内容。(例如phonenumber=phonenumberarray[indexath.row]这是func tableView(u tableView:UITableView,cellForRowAt indexath:indexath)->UITableViewCell中的内容{let cell=tableView.deque…cell.BtnInfo.addTarget'您可以启用所有的单元格信息按钮,并在视图中启用一个目标控制器,该控制器可以使用正确的信息弹出显示,因为不需要两次点击。根据所选单元格的indexPath,我编写了有关从数组获取数据的所有内容。(例如phonenumber=phonenumberarray[indexPath.row]这是“func tableView(u tableView:UITableView,cellForRowAt indexPath:indexPath)->UITableViewCell{let cell=tableView.deque…cell.BtnInfo.addTarget”中的内容