Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中使用多节从UITableView中删除单元格_Ios_Iphone_Swift - Fatal编程技术网

Ios 如何在swift中使用多节从UITableView中删除单元格

Ios 如何在swift中使用多节从UITableView中删除单元格,ios,iphone,swift,Ios,Iphone,Swift,我试图在swift中从UITableView中删除单元格,我遵循以下教程: 问题是我的UITableView有很多部分,所以我不能像教程那样删除单元格。 有人知道如何删除具有多个节的单元格表单表吗? 谢谢。您不能使用本教程中介绍的方法一次删除多个单元格。这只适用于单个电池。例如,如果选择多个单元格并使用按钮触发删除操作,则代码可能如下所示: if let indexPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] {

我试图在swift中从UITableView中删除单元格,我遵循以下教程: 问题是我的UITableView有很多部分,所以我不能像教程那样删除单元格。 有人知道如何删除具有多个节的单元格表单表吗?
谢谢。

您不能使用本教程中介绍的方法一次删除多个单元格。这只适用于单个电池。例如,如果选择多个单元格并使用按钮触发删除操作,则代码可能如下所示:

if let indexPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] {
    for indexPath in indexPaths {
        // one by one remove items from your datasource
    }

    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
} 

不能使用教程中描述的方法一次删除多个单元格。这只适用于单个电池。例如,如果选择多个单元格并使用按钮触发删除操作,则代码可能如下所示:

if let indexPaths = tableView.indexPathsForSelectedRows() as? [NSIndexPath] {
    for indexPath in indexPaths {
        // one by one remove items from your datasource
    }

    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
} 

在本例中,您可以使用数字[section][row],而不是使用数字[row]。因此,代码如下所示:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numbers[section].count
  }

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

在本例中,您可以使用数字[section][row],而不是使用数字[row]。因此,代码如下所示:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numbers[section].count
  }

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

这两个答案对我都不起作用。Swift数组索引在删除时会更新,因此对于中的索引,在循环中进行更新。indexPathsForSelectedRows提供了意外的结果,即删除了错误的数据/表,并最终因索引超出数组边界错误而崩溃。发现很好但确实过时的Objective-C。但它使用了NSMutableArray RemoveObjectSatiIndexes方法,而Swift数组中没有这种方法。无论如何,这里有很多有用的技巧,值得一看。 适用于我的方法是该示例的一部分,但它不是RemoveObjectSatiIndexes do while,而是用于逐个删除行,直到删除所有选定行。UIAlertAction调用下面的方法类似于Apple示例

  func deleteSelectedRows() {

    // Unwrap indexPaths to check if rows are selected
    if let _ = tableView.indexPathsForSelectedRows() {
      // Do while all selected rows are deleted
      do {

      if let indexPath = tableView.indexPathForSelectedRow(){
        //remove from table view data source and table view
        self.dataSource.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        }

      } while tableView.indexPathsForSelectedRows() != nil

    }else{
      // Delete everything, delete the objects from data model.
      self.dataSource.removeAll(keepCapacity: false)

      // Tell the tableView that we deleted the objects.
      // Because we are deleting all the rows, just reload the current table section
      self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
    }
    // Exit editing mode after the deletion.
    self.tableView.setEditing(false, animated: true)
  }
编辑:作为一个小例子,我一直在使用JU,从swift开始,它没有效率。要么扩展数组,要么使数据源相等,最好使用find或.filter。但我相信应该有一种更简单的方法。下面的链接描述了我现在使用的一个:

然后:

if let selectedRows = tableView.indexPathsForSelectedRows(){
  var objectsToDelete = [myDataSource]()
  for selectedRow in selectedRows {
    objectsToDelete.append(myDataSource[selectedRow.row])
  }
  for object in objectsToDelete {
    if let index = find(myDataSource, object){
      myDataSource.removeAtIndex(index)
    }
  }
}
tableView.deleteRowsAtIndexPaths([selectedRows], withRowAnimation: .Automatic)

这两个答案对我都不起作用。Swift数组索引在删除时会更新,因此对于中的索引,在循环中进行更新。indexPathsForSelectedRows提供了意外的结果,即删除了错误的数据/表,并最终因索引超出数组边界错误而崩溃。发现很好但确实过时的Objective-C。但它使用了NSMutableArray RemoveObjectSatiIndexes方法,而Swift数组中没有这种方法。无论如何,这里有很多有用的技巧,值得一看。 适用于我的方法是该示例的一部分,但它不是RemoveObjectSatiIndexes do while,而是用于逐个删除行,直到删除所有选定行。UIAlertAction调用下面的方法类似于Apple示例

  func deleteSelectedRows() {

    // Unwrap indexPaths to check if rows are selected
    if let _ = tableView.indexPathsForSelectedRows() {
      // Do while all selected rows are deleted
      do {

      if let indexPath = tableView.indexPathForSelectedRow(){
        //remove from table view data source and table view
        self.dataSource.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        }

      } while tableView.indexPathsForSelectedRows() != nil

    }else{
      // Delete everything, delete the objects from data model.
      self.dataSource.removeAll(keepCapacity: false)

      // Tell the tableView that we deleted the objects.
      // Because we are deleting all the rows, just reload the current table section
      self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
    }
    // Exit editing mode after the deletion.
    self.tableView.setEditing(false, animated: true)
  }
编辑:作为一个小例子,我一直在使用JU,从swift开始,它没有效率。要么扩展数组,要么使数据源相等,最好使用find或.filter。但我相信应该有一种更简单的方法。下面的链接描述了我现在使用的一个:

然后:

if let selectedRows = tableView.indexPathsForSelectedRows(){
  var objectsToDelete = [myDataSource]()
  for selectedRow in selectedRows {
    objectsToDelete.append(myDataSource[selectedRow.row])
  }
  for object in objectsToDelete {
    if let index = find(myDataSource, object){
      myDataSource.removeAtIndex(index)
    }
  }
}
tableView.deleteRowsAtIndexPaths([selectedRows], withRowAnimation: .Automatic)

试试这个。这个很好用。 但别忘了在这之前。 func tableview tableview:UITableView,canEditRowAtIndexPath indexath:nsindepath->Bool { 返回真值 }


试试这个。这个很好用。 但别忘了在这之前。 func tableview tableview:UITableView,canEditRowAtIndexPath indexath:nsindepath->Bool { 返回真值 }


当你尝试时,哪里出了问题?显示您的代码。尝试时出现了什么问题?显示您的代码。谢谢您的帮助。但我在运行应用程序时遇到一个异常,异常是:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“无效更新:节0中的行数无效。”。更新1后现有节中包含的行数必须等于更新1前该节中包含的行数,加上或减去从该节中插入或删除的行数0 inserted,1 deleted,以及加上或减去移入或移出该节的行数0 moved in,0搬出去。“为什么?谢谢你的帮助。”。但我在运行应用程序时遇到一个异常,异常是:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“无效更新:节0中的行数无效。”。更新1后现有节中包含的行数必须等于更新1前该节中包含的行数,加上或减去从该节中插入或删除的行数0 inserted,1 deleted,加上或减去移入或移出该节的行数0 moved in,0 moved out。'为什么?