ios - 快速删除表格 View 中的一行

标签 ios uitableview swift

我正在尝试删除 TableView 中的一行。我已经实现了所需的方法,但是当我水平滑动该行时,没有删除按钮出现。我进行了搜索,并且到处都找到了相同的解决方案,但它对我的情况不起作用。我不知道我在哪里犯了错误。谁能帮帮我吗?

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()
   }
}

你必须刷新表格。

关于ios - 快速删除表格 View 中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294099/

相关文章:

ios - Metal alpha 混合不起作用

ios - 创建自定义 UI 栏按钮(从 ListView 、 GridView 更改 View )

ios - Swift malloc_error_break 因双重释放而崩溃

ios - 比较空的 NSURL

ios - 使用 UISegmentedControl 改变 Table View 的数据

iphone - iOS iPhone 5 选择正确的 Storyboard

ios - 默认详细 View uisplitview

ios - Swift segue 不会将数据传递给旧的 ViewController

ios - swift:长按手势识别器不起作用

ios - 如何从 GameViewController 中的另一个 swift 文件调用方法?