2017-02-25 61 views
0

我在插入到tableView的自定义单元格中有一个切换按钮。如何更新切换按钮的切换表格视图?如何调用自定义单元格中的开关切换功能 - TableViewCell Swift

下面是我在哪里插入包含toggleSwitch

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

+0

如果您需要确定哪些小区切换的'UISwitch'然后在您的自定义添加一个'目标/ action' cell的子类,然后使用委托向您发送一条消息,以'cell'作为参数来查看控制器。另外,你为什么不在你的'tableView(_ :, cellForRowAt:)'方法中使用'dequeueReusableCell(withIdentifier :, for:)'? –

回答

4

UISwitchUIControl子类,允许你设置它的目标/行动自定义tableViewCell功能:

在您的UIViewController中执行以下功能:

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

在你cellForRowAt:您现在可以添加目标/行动细胞的UISwitch

cell.availabilitySwitch.addTarget(self, action: #selector(toggled:), for: .valueChanged) 
相关问题