2015-10-04 81 views
0

我試圖實現一個鬧鐘應用程序就像蘋果的時鐘應用程序。點擊左側的編輯按鈕,我想使表進入編輯模式,紅圈在每個單元格的左側(自定義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

你應該實現 tableView(_:editingStyleForRowAtIndexPath:)並返回.Delete

+0

我實現了,但它並沒有任何區別。 self.navigationItem.leftBarButtonItem = self.editButtonItem()//點擊編輯按鈕完成按鈕,但表格不進入編輯模式。但是,當我在編輯按鈕上設置** @ IBAction **時,出現紅色圓圈按鈕,但在滑動時不顯示任何事件。每當我嘗試滑動單元格時,我也會收到此消息:** MyApp [944:17497]必須有滑動才能刪除確認視圖,以便在安裝滑動刪除gobbler時** – user5407225