【问题标题】:Not able to show Delete button无法显示删除按钮
【发布时间】:2015-10-04 17:28:13
【问题描述】:

我正在尝试实现一个闹钟应用就像苹果的时钟应用。点击左侧的编辑按钮,我想让表格进入红色编辑模式每个单元格左侧的圆圈(自定义 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)

}

}

【问题讨论】:

    标签: swift2 ios9 xcode7


    【解决方案1】:

    你应该实现 tableView(_:editingStyleForRowAtIndexPath:)并返回.Delete

    【讨论】:

    • 我实现了,但没有任何区别。 self.navigationItem.leftBarButtonItem = self.editButtonItem() // 点击编辑按钮完成按钮出现,但表格没有进入编辑模式。但是,当我在 Edit 按钮上设置 @IBAction 时,会出现红色圆圈按钮,但在 Swipe 上没有事件。每当我尝试滑动单元格时,我也会收到此消息:MyApp[944:17497] must have a swipe to delete Confirm view when install the swipe to delete gobbler
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-22
    相关资源
    最近更新 更多