表视图:不显示从右向左滑动以删除-swift 3
我用Swift 3构建了一个简单的toDoList应用程序。现在,我想通过从右向左滑动,从表视图:不显示从右向左滑动以删除-swift 3,swift,tableview,swift3,ios10,Swift,Tableview,Swift3,Ios10,我用Swift 3构建了一个简单的toDoList应用程序。现在,我想通过从右向左滑动,从表视图中删除我的项目。这是我找到的代码。但是当我向左滑动时,什么也没有发生 代码: func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return toDoList.count } func tableView(_ tableView: UITableView, can
表视图中删除我的项目。这是我找到的代码。但是当我向左滑动时,什么也没有发生
代码:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toDoList.count
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = toDoList[indexPath.row]
return cell
}
//
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
toDoList.remove(at: indexPath.row)
UserDefaults.standard.set(toDoList, forKey: "toDoList")
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
这仍然不起作用向左滑动时不会发生任何情况。待办事项清单本身正在工作。我可以将项目添加到表中,但无法删除它们
谢谢:)您是否实现了该方法
编辑:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toDoList.count
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = toDoList[indexPath.row]
return cell
}
//
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
toDoList.remove(at: indexPath.row)
UserDefaults.standard.set(toDoList, forKey: "toDoList")
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
感谢@rmaddy提到tableView:caneditrowatinexpath:
的默认值是true
,实现它并不能解决问题
我不太确定您想从代码片段中做什么,所以请确保您正在实现以下方法():
您还可以保留tableView:caneditrowatinexpath:
method:
要求数据源验证给定行是否可编辑
因此,-例如-如果您想让第一行不可编辑,即用户无法滑动和删除第一行,您应该执行以下操作:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == 0 {
return false
}
return true
}
确保UITableViewDataSource
和UITableViewDelegate
与ViewController连接。是否实现了该方法
编辑:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return toDoList.count
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
cell.textLabel?.text = toDoList[indexPath.row]
return cell
}
//
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
toDoList.remove(at: indexPath.row)
UserDefaults.standard.set(toDoList, forKey: "toDoList")
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
感谢@rmaddy提到tableView:caneditrowatinexpath:
的默认值是true
,实现它并不能解决问题
我不太确定您想从代码片段中做什么,所以请确保您正在实现以下方法():
您还可以保留tableView:caneditrowatinexpath:
method:
要求数据源验证给定行是否可编辑
因此,-例如-如果您想让第一行不可编辑,即用户无法滑动和删除第一行,您应该执行以下操作:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == 0 {
return false
}
return true
}
确保UITableViewDataSource
和UITableViewDelegate
与ViewController连接。实施CaneditRowatineXpath您需要通过修复发布的代码来更新问题。代码的开头毫无意义。请尝试检查是否有任何手势识别器正在使用刷卡事件。(你的应用程序中有滑动菜单吗?)。如果你能更新你的代码也会更好。@ELKA我没有任何手势识别器,也没有滑动菜单。更新代码实现CaneditRowatineXpath您需要通过修复发布的代码来更新您的问题。代码的开头毫无意义。请尝试检查是否有任何手势识别器正在使用刷卡事件。(你的应用程序中有滑动菜单吗?)。如果你能更新你的代码也会更好。@ELKA我没有任何手势识别器,也没有滑动菜单。更新了代码如果未实现此委托方法,则默认为true
,因此这不会是问题所在。UITableViewDataSource和UITableViewDelegate已连接。我实现了editingStyleForRowAt indexPath函数。仍然不起作用。如果不实现此委托方法,它将默认为true
,因此这不会是问题所在。UITableViewDataSource和UITableViewDelegate已连接。我实现了editingStyleForRowAt indexPath函数。还是不行。