0

I am trying to implement an alarm clock app just like apple's Clock app.On the click of Edit button on the left hand side I want to make table enter into Editing mode with red circles on left side of every cell(custom UITableViewCell) And on the click of that red circle want to show "Delete" button/action on the right side.

I have been trying a lot and went through many sites but still could not figure out. Can someone please see what mistake I am making?

I have referred below and many others links: 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)

}

}
Community
  • 1
  • 1
user5407225
  • 41
  • 1
  • 5

1 Answers1

0

You should implement tableView(_:editingStyleForRowAtIndexPath:) and return .Delete

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • I implemented but it did not any difference. self.navigationItem.leftBarButtonItem = self.editButtonItem() // on the tap of Edit button Done button appears but table does not go into editing mode. But when I set the **@IBAction** on Edit button then red circled button appears but no event on Swipe. I am also getting this message whenever I tried to Swipe the cell: **MyApp[944:17497] must have a swipe to delete confirmation view when installing the swipe to delete gobbler** – user5407225 Oct 04 '15 at 23:10