如何在自定义单元格中切换开关切换函数 - TableViewCell Swift

时间:2017-02-25 15:27:11

标签: ios swift uitableview

我在自定义单元格中有一个切换按钮,插入到tableView中。如何在切换按钮切换时更新tableview?

下面是我插入包含toggleSwitch

的自定义tableViewCell的函数
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
  let cell = Bundle.main.loadNibNamed("AvailabilityTableViewCell", owner: self, options: nil)?.first as! AvailabilityTableViewCell
            
  // Configure the cell...
  cell.availabilityLabel.text = "Available"
  switchIsOn = cell.availabilitySwitch.isOn
  return cell
            
 }

enter image description here

1 个答案:

答案 0 :(得分:5)

UISwitchUIControl的子类,允许您在其上设置目标/操作:

UIViewController中实施以下功能:

func toggled(sender: UISwitch) {
   // React to switch being toggled here
}

在您的cellForRowAt:中,您现在可以将目标/操作添加到单元格UISwitch

cell.availabilitySwitch.addTarget(self, action: #selector(toggled:), for: .valueChanged)