UISwitch
をSwiftのtableViewセルにプログラムで埋め込むにはどうすればよいですか? は、私はここでそのSwiftでUITableViewセルのスイッチを追加
let shareLocationSwitch = UISwitch()
cell.accessoryView = shareLocationSwitch
UISwitch
をSwiftのtableViewセルにプログラムで埋め込むにはどうすればよいですか? は、私はここでそのSwiftでUITableViewセルのスイッチを追加
let shareLocationSwitch = UISwitch()
cell.accessoryView = shareLocationSwitch
ようにそれをやっているあなたはUITableView
セルにUISwitch
を埋め込むことができる方法です。
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
awakeFromNib
UITableViewCell
内部メソッドの代わりに、テーブルビューcellForRowAt
方法を設定する必要があります。デキューすると、再利用可能なcell
あなたCustomCellにキャストHere is sample from @LeoDabus
それだけでUISwitch()などの変数を作成し、あなたがそこに着くために多くのものを行う必要はありませんので で細胞accesooryViewを呼び出すので、シンプルで簡単です。コードはすでに質問の中にあります。 – TAO
質問を編集するだけです。 ソリューションに感謝します。 – TAO
なぜ私の質問は-2ポイントを受け取った、それは本当にシンプルで、私はちょうどそれを送信し、誰も混乱しないように質問を編集良いソリューションを見つけた後。 – TAO