Ios 在swift中删除表视图中的行
我正在尝试删除表视图中的一行。我已经实现了所需的方法,但是当我水平滑动行时,没有出现删除按钮。我到处寻找,找到了相同的解决方案,但在我的案例中却不起作用。我不知道我在哪里犯了错误。有人能帮我吗Ios 在swift中删除表视图中的行,ios,uitableview,swift,Ios,Uitableview,Swift,我正在尝试删除表视图中的一行。我已经实现了所需的方法,但是当我水平滑动行时,没有出现删除按钮。我到处寻找,找到了相同的解决方案,但在我的案例中却不起作用。我不知道我在哪里犯了错误。有人能帮我吗 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableVi
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()
}
}
您必须刷新表格。将自动布局约束应用于表格视图。删除按钮在那里,但没有显示,因为没有自动布局我也在努力解决这个问题,但最终找到了一个解决方案-我正在运行XCode 8.3.2。我读到的很多答案都是针对旧版本的XCode/Swift的,很多是针对Objective-C的
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
yourArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
我找到的解决方案是对UITableView使用“EditActionsErrorwat”工具。注意:我读到的所有其他内容都在不断地指向UITableView的“commit editingStyle”工具,但我始终无法让它工作。使用editActionsForRowAt对我来说非常有效
注意:“postData”是我添加数据到的数组的名称
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]
}
编辑:固定格式,因此代码显示在灰色框中:)用于swift 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
}
}
我想你应该检查函数syntaxfunc tableView(tableView:UITableView,committeeditingstyle编辑样式:UITableView单元格编辑样式,forRowAtIndexPath indexath:nsindexath){@LeonardoSavioDabus我使用了与u建议相同的语法,我也更新了我的问题,但仍然没有删除按钮。我发现@Nilesh的以下答案有效。我缺少查看删除按钮的约束。我使用的是表视图而不是表视图控制器,我不能使用覆盖,我已经实现了该方法以同样的方式,但它不起作用。我有(一些)限制,但显然还不够。自动布局是一门艺术,而不是一门科学。:)tableView.deleteRowsAtIndexPaths([indexPath],带RowAnimation:.Fade)比重新加载整个表格读取重新加载数据()更好。如Danny所说,使用“deleteRowsAtIndexPaths”代替beginUpdate()endUpdate()解决了我在Swift 3中尝试调用DeleteRow时应用程序崩溃的问题Commit EditingStyle在Swift 3中对我有效..在他们的网站上发布了一个工作示例..你的方法对我也有效。看起来更好。
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]
}
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
}
}