只是学习所以我确信有更好的方法来编码我到目前为止所拥有的。无论如何,我有一个简单的任务列表保存到核心数据。您可以转到另一个可以编辑或创建新任务的VC。我希望能够在自定义单元格中放置一个uiswitch,允许您将任务设置为完成,然后重新加载tableview。我已经搜索了所有这些Select UITableView's row when tapping on its UISwitch in Swift,但似乎无法翻译解决方案。任何帮助都会很棒。
我不确定我是应该在自定义单元格中编写保存代码,还是仅仅代理或传递代理。
这是我的自定义单元格 protocol SettingCellDelegate:class { func didChangeSwitchState(发送者:TaskCell,isOn:Bool) }
类TaskCell:UITableViewCell {
var delegate: SettingCellDelegate!
@IBOutlet weak var taskImage: UIImageView!
@IBOutlet weak var arrowImage: UIImageView!
@IBOutlet weak var dueDateLabel: UILabel!
@IBOutlet weak var dueDateValue: UILabel!
@IBOutlet weak var dueTimeLabel: UILabel!
@IBOutlet weak var dueTimeValue: UILabel!
@IBOutlet var taskCellStatus: UITextField!
@IBOutlet var taskCellSwitchDisplay: UISwitch!
@IBAction func taskCellSwitchAction(sender: AnyObject) {
if (taskCellSwitchDisplay.on) {
taskCellStatus.text = "Completed"
} else {
taskCellStatus.text = "Active"
}
}
@IBOutlet weak var taskNameValue: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
taskCellSwitchDisplay.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
}
func configureCell(task: Tasks) {
taskImage.image = UIImage(named: "Icon.png")
arrowImage.image = UIImage(named: "arrow_filled.png")
dueDateLabel.text = "Due Date:"
dueDateValue.text = task.taskDate
dueTimeLabel.text = "Due Time:"
dueTimeValue.text = task.taskTime
taskNameValue.text = task.taskName
taskCellStatus.text = task.status
//taskCellStatus.hidden = true
if (taskCellStatus.text == "Completed") {
taskCellSwitchDisplay.on = true
} else {
taskCellSwitchDisplay.on = false
}
}
func switchChanged(taskCellSwitchDisplay: UISwitch) {
_ = taskCellSwitchDisplay.on
if taskCellSwitchDisplay.on == true {
let switchValue = "Completed"
print("Switch Variable: \(switchValue)")
delegate.didChangeSwitchState(self, isOn: true)
} else {
let switchValue = "Active"
print("Switch Variable: \(switchValue)")
delegate.didChangeSwitchState(self, isOn: false)
}
}
这是我在tableview中获取委托的地方
扩展TaskCell:SettingCellDelegate { func didChangeSwitchState(发送者:TaskCell,isOn:Bool){
if isOn == true {
print("true")
} else {
print("false")
}
}