在名为'Item'的实体中有两个属性'time'和'isOn'(string,bool)
在viewcontroller类中的我可以将默认条件赋予'isOn'属性(在savePressed函数中),这使得switchbtn.isOn = true并将其保存在特定'time'的数据模型中
viewcontroller类: -
class ViewController: UIViewController {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
@IBOutlet weak var timePickerView: UIDatePicker!
@IBOutlet weak var timeLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
timePickerView.setValue(UIColor.white, forKeyPath: "textColor")
dateFormat()
// Do any additional setup after loading the view.
}
@IBAction func savePressed(_ sender: UIBarButtonItem) {
let entity = Item(context: context)
entity.time = timeLbl.text
entity.isOn = true
saveData()
self.dismiss(animated: true, completion: nil)
}
@IBAction func cancelPressed(_ sender: UIBarButtonItem) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func valueChanged(sender:UIDatePicker, forEvent event: UIEvent){
dateFormat()
}
func saveData() {
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
func dateFormat() {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
formatter.timeStyle = .short
timeLbl.text = formatter.string(from: timePickerView.date)
}
}
在这个类中,我能够获取并显示核心数据,但不知道如何保存单元格切换按钮的状态并更新数据模型,因为没有使用'didSelectRowAt'函数
tableview类: -
class TableViewController: UITableViewController {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var items = [Item]()
override func viewDidLoad() {
super.viewDidLoad()
print(arr)
}
override func viewWillAppear(_ animated: Bool) {
getData()
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell
cell.timeLbl.text = items[indexPath.row].time
cell.switchBtn.isOn = items[indexPath.row].isOn
return cell
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
items = try context.fetch(Item.fetchRequest())
}catch{
print("failed to get the data")
}
}
}
在此我可以打印交换机的当前状态但无法从tableview类访问'items [indexPath.row]'
细胞类: -
class TableViewCell: UITableViewCell {
@IBOutlet weak var timeLbl: UILabel!
@IBOutlet weak var switchBtn: UISwitch!
var alarm = Bool()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func valChange(_ sender: UISwitch) {
if sender.isOn{
switchBtn.isOn = true
}else {
switchBtn.isOn = false
}
}
}
答案 0 :(得分:0)
在Swift中,最有效的方法是回调闭包。
在单元格中添加一个属性callback
,其中一个闭包传递Bool
值且没有返回值。当交换机的值发生变化时调用回调。
class TableViewCell: UITableViewCell {
@IBOutlet weak var timeLbl: UILabel!
@IBOutlet weak var switchBtn: UISwitch!
var alarm = Bool()
var callback : ((Bool)->())?
@IBAction func valChange(_ sender: UISwitch) {
callback?(sender.isOn)
}
}
在控制器的cellForRow
中添加回调,在闭包中更新模型。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell
let item = items[indexPath.row]
cell.timeLbl.text = item.time
cell.switchBtn.isOn = item.isOn
cell.callback = { newValue in
item.isOn = newValue
}
return cell
}