在swift中删除表视图中的一行

我试图在表视图中删除一行。 我已经实现了所需的方法但是当我横向滑动行时没有删除按钮。 我已经搜索过,而且我到处都找到了相同的解决方案,但它在我的情况下不起作用。 我不知道我在哪里弄错了。 有人可以帮我吗?

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { dataHandler.deletePeripheral(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } 

如果以下编码对您有帮助,我会很高兴

 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { yourArray.removeAtIndex(indexPath.row) self.tableView.reloadData() } } 

SWIFT 3.0

 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { yourArray.remove(at: indexPath.row) tblDltRow.reloadData() } } 

你必须刷新表。

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if (editingStyle == UITableViewCellEditingStyle.Delete) { self.tableView.beginUpdates() self.arrayData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists. self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 2)), withRowAnimation: UITableViewRowAnimation.Left) self.tableView.endUpdates() } 

将Autolayout约束应用于表视图。 由于没有自动布局,删除按钮在那里但没有显示

 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == UITableViewCellEditingStyle.Delete { numbers.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) } } 

我也很努力,但终于找到了解决方案 – 我正在运行XCode 8.3.2。 我读到的很多答案都是针对旧版本的XCode / Swift而且很多是针对Objective-C的。

我找到的解决方案是使用UITableViews的’editActionsForRowAt’工具。 注意:我读到的其他内容一直指向UITableView的’ commit editingStyle ‘工具,但我永远无法让它工作。 使用editActionsForRowAt对我来说非常合适。

注意:’postData’是我添加数据的Array的名称。

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in // Remove item from the array postData.remove(at: IndexPath.row) // Delete the row from the table view tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade) }) // set DeleteAction Button to RED (this line is optional - it simply allows you to change the color of the Delete button - remove it if you wish) deleteAction.backgroundColor = UIColor.red return [deleteAction] } 

我还有一些代码在“删除”按钮旁边添加“编辑按钮”:

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, IndexPath) in //print("Edit tapped") }) // Set EditAction button to blue (this line is not mandatory - it just sets the color of the Edit box to blue) editAction.backgroundColor = UIColor.blue let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in //print("Delete tapped") // Remove item from the array postData.remove(at: IndexPath.row) // Delete the row from the table view tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade) }) // set DeleteAction Button to RED (this line isn't mandatory - it just sets the color of the Delete box) deleteAction.backgroundColor = UIColor.green return [editAction, deleteAction] } 

编辑:固定格式所以代码出现在灰色框:)

对于斯威夫特3

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == UITableViewCellEditingStyle.delete { dataHandler.deletePeripheral(indexPath.row) tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) // - OR - // mTableView.reloadData() //mTableView is an outlet for the tableView } } 

我知道你有什么问题。 我想你只是检查你的表约束他们可能是错的只需重新检查你的自动布局约束

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == UITableViewCellEditingStyle.Delete { yourArray.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) } } 
 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { carsArray.removeAtIndex(indexPath.row) self.tableView.reloadData() } }