我正在尝试实施一个闹钟应用就像苹果的时钟应用。点击左侧的编辑按钮,我想让表进入编辑模式在每个单元格的左侧有红色圆圈(自定义UITableViewCell)并且点击该红色圆圈想要显示"删除"按钮/右侧的操作。
我一直在尝试很多并经历了很多网站,但仍然无法弄清楚。有人可以看看我犯了什么错误吗?
我在下面提到了许多其他链接: How to enable swipe to delete cell in a TableView? UITableViewCell, show delete button on swipe
class SavedAlarmListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
var alarms = [AlarmDataObject]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.navigationItem.leftBarButtonItem = self.editButtonItem()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
refreshList()
}
func refreshList() {
alarms = AlarmsList.sharedInstance.allSavedAlarms()
tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return alarms.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AlarmTableViewCell
// custom code to set data ....
return cell
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true // all cells are editable
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let alarm = alarms.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
AlarmsList.sharedInstance.removeAnAlarm(alarm)
}
}
}
class AlarmTableViewCell:UITableViewCell {
// IBOutlets
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
答案 0 :(得分:0)
您应该实施
tableView(_:editingStyleForRowAtIndexPath:)
并返回.Delete