将表格视图中的选定单元格添加到其他部分

时间:2019-03-19 05:32:12

标签: ios swift uitableview

我在表格视图中显示了一些行,就像这样... enter image description here

在这里,单击复选框按钮(在每个单元格的右侧),我想将该单元格添加到其他部分,以便所选单元格将出现在一个部分中,而未选中的单元格将出现在另一个部分中这样的部分...

enter image description here

在点击复选框按钮时,我已经尝试过类似的操作...

  func productTap(cell: ProductTagTableViewCell) {
    if let indexPath = tableview?.indexPath(for: cell) {
      print("indexPath.row: \(indexPath.row)")

  let prodItem = self.arrData[indexPath.row]
  selectedItems.append(prodItem)
  self.tableview.beginUpdates()

  // Creating indexpath for the new item
  let indexPath = IndexPath(row: selectedItems.count - 1, section: 0)
  tableview.insertRows(at: [indexPath], with: .automatic)
  tableview.endUpdates()

  }
}

但是它只会因错误而崩溃。

  

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效的更新:第0节中的行数无效。更新(3)后现有节中包含的行数必须等于行数该部分包含在更新之前(3),....

编辑1:。这就是我的模型的外观。

class NewModel {
  var heading : String
  var subHeading : String
  var selected = false

  init(heading : String, subHeading : String) {
    self.heading = heading
    self.subHeading = subHeading

  }

  class func getModelData() -> [NewModel] {

    let array = [NewModel(heading: “This is heading1“, subHeading: "This is sub heading1"),
                 NewModel(heading: "This is heading2”, subHeading: "This is sub heading2”),
                 NewModel(heading: "This is heading3”, subHeading: "This is sub heading3”)]

    return array
  }
}

编辑2 新更改:

这是我的tableview委托方法:

 func numberOfSections(in tableView: UITableView) -> Int {
    return 2
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var count: Int?
    if section == 0 {
      count = self.arrData2.count
    }
    else if section == 1 {
      return self.arrData.count
    }
    return count!
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 93.0
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: ProductTagTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellID) as! ProductTagTableViewCell
    cell.delegate = self
    cell.selectionStyle = .none

    switch (indexPath.section) {
    case 0:
      cell.productNameLabel.text = self.arrData2[indexPath.row].heading
      cell.priceLabel.text = self.arrData2[indexPath.row].subHeading

    case 1:
      cell.productNameLabel.text = self.arrData[indexPath.row].heading
      cell.priceLabel.text = self.arrData[indexPath.row].subHeading

    default:
      cell.productNameLabel.text = self.arrData[indexPath.row].heading
      cell.priceLabel.text = self.arrData[indexPath.row].subHeading

    }
    return cell
  }

在复选框按钮上,我执行了此操作...

func productTap(cell: ProductTagTableViewCell) {
    if let indexPath = tableview?.indexPath(for: cell) {

      self.arrData2.append(self.arrData[indexPath.row])
      self.arrData.remove(at: indexPath.row)
      tableview.deleteRows(at: [indexPath], with: .automatic)
      tableview.beginUpdates()
      tableview.insertRows(at: [IndexPath(row: arrData2.count-1, section: 0)], with: .automatic)
      self.tableview.endUpdates()
   }
}

1 个答案:

答案 0 :(得分:0)

请,请使用更有意义的变量名。 arrDataarrData2令人震惊和混乱。

例如命名数组

var deselectedProducts = [NewModel]()
var selectedProducts = [NewModel]()

要将项目从deselected移至selected写入

func productTap(cell: ProductTagTableViewCell) {
    if let indexPath = tableview?.indexPath(for: cell) {
       let itemToMove = deselectedProducts.remove(at: indexPath.row) 
       let insertionIndexPath = IndexPath(row: selectedProducts.count, section: 0)
       selectedProducts.append(itemToMove)
       tableview.beginUpdates()
       tableview!.deleteRows(at: [indexPath], with: .automatic)
       tableview!.insertRows(at: [insertionIndexPath], with: .automatic)
       self.tableview.endUpdates()
   }
}

通过这种方式,您可以将numberOfRowsInSection中的代码简化为

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return (section == 0) ? selectedProducts.count : deselectedProducts.count
}
相关问题