这是您可以
UISwitch在
UITableView单元格上嵌入的方法。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifire", for: indexPath) as! YourCellClass //here is programatically switch make to the table view let switchView = UISwitch(frame: .zero) switchView.setOn(false, animated: true) switchView.tag = indexPath.row // for detect which row switch Changed switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged) cell.accessoryView = switchView return cell }这是切换调用贝克方法
func switchChanged(_ sender : UISwitch!){ print("table row switch Changed (sender.tag)") print("The switch is (sender.isOn ? "ON" : "OFF")")}@LeoDabus
Great! explanation。
注意:如果您tableview
可能有多个,section
则应创建一个CustomCell子类UITableViewCell
并配置您的accessoryView
内部UITableViewCell
awakeFromNib
方法而不是表视图cellForRowAt
方法。当将可重用cell
的队列释放出队列时,将其投射到您的CustomCell中这是@LeoDabus的示例



