Table 视图:从右向左滑动删除不显示 - swift 3
Table View: right to left swipe to delete does not show up - swift 3
我用 Swift 构建了一个简单的 toDoList 应用程序 3. 现在我希望能够通过从右向左滑动来从 TableView
中删除我的项目。这段代码是我找到的。但是当我向左滑动时没有发生。
代码:
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
}
这还是不行。 向左滑动时没有任何反应。待办事项列表本身正在运行。我可以向 table 添加项目,但我无法删除它们。
谢谢:)
你实现了tableView:canEditRowAtIndexPath:方法了吗?
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
编辑:
感谢@rmaddy 提到 tableView:canEditRowAtIndexPath:
的默认值是 true
,实现它并不能解决问题。
我不太确定你想从你的代码片段中做什么,所以请确保你正在实施以下方法 (UITableViewDelegate):
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
}
你也可以保留tableView:canEditRowAtIndexPath:
方法的实现:
Asks the data source to verify that the given row is editable.
因此,-例如-如果您想让第一行不可编辑,即用户无法滑动和删除第一行,您应该执行以下操作:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == 0 {
return false
}
return true
}
确保 UITableViewDataSource
和 UITableViewDelegate
与 ViewController 连接。
我用 Swift 构建了一个简单的 toDoList 应用程序 3. 现在我希望能够通过从右向左滑动来从 TableView
中删除我的项目。这段代码是我找到的。但是当我向左滑动时没有发生。
代码:
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
}
这还是不行。 向左滑动时没有任何反应。待办事项列表本身正在运行。我可以向 table 添加项目,但我无法删除它们。
谢谢:)
你实现了tableView:canEditRowAtIndexPath:方法了吗?
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
编辑:
感谢@rmaddy 提到 tableView:canEditRowAtIndexPath:
的默认值是 true
,实现它并不能解决问题。
我不太确定你想从你的代码片段中做什么,所以请确保你正在实施以下方法 (UITableViewDelegate):
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
}
你也可以保留tableView:canEditRowAtIndexPath:
方法的实现:
Asks the data source to verify that the given row is editable.
因此,-例如-如果您想让第一行不可编辑,即用户无法滑动和删除第一行,您应该执行以下操作:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == 0 {
return false
}
return true
}
确保 UITableViewDataSource
和 UITableViewDelegate
与 ViewController 连接。