Swift-tableView中的可移动行仅在一个区域内,而不在两个区域之间

时间:2018-09-09 08:58:03

标签: ios swift uitableview

有没有一种方法可以防止tableView中的单元格移动到其他部分?

sections包含用于不同类型单元格的数据,因此,当用户尝试将一个单元格拖动到其他部分时,该应用程序将崩溃。

我只允许用户在部分内移动单元格,而不能在部分之间移动单元格。

相关代码如下:

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let reorderedRow = self.sections[sourceIndexPath.section].rows.remove(at: sourceIndexPath.row)
    self.sections[destinationIndexPath.section].rows.insert(reorderedRow, at: destinationIndexPath.row)

    self.sortedSections.insert(sourceIndexPath.section)
    self.sortedSections.insert(destinationIndexPath.section)
}

3 个答案:

答案 0 :(得分:2)

您将需要实现UITableViewDelegate方法targetIndexPathForMoveFromRowAt

如果源section与目的地相同,则您的策略是允许移动。如果不是,那么如果建议的目标节小于源节,则可以返回第0行,如果建议的目标节大于源节,则可以返回节的最后一行。

这将限制移动到源代码部分。

override func tableview(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {

    let sourceSection = sourceIndexPath.section
    let destSection = proposedDestinationIndexPath.section

    if destSection < sourceSection {
        return IndexPath(row: 0, section: sourceSection)
    } else if destSection > sourceSection {
        return IndexPath(row: self.tableView(tableView, numberOfRowsInSection:sourceSection)-1, section: sourceSection)
    }

    return proposedDestinationIndexPath
}

答案 1 :(得分:0)

检查此苹果文档链接

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html

您需要在其中管理条件

targetIndexPathForMoveFromRowAtIndexPath

canMoveRowAtIndexPath

答案 2 :(得分:0)

您可以通过实现tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:方法来重新定位提议的目标以进行限制

  func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {

    // Finds number of items in source group
    let numberOfItems = self.tableView(tableView, numberOfRowsInSection: sourceIndexPath.section)

    // Restricts rows to relocation in their own group by checking source and destination sections
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {

      /*
       if we move the row to the not allowed upper area, it is moved to the top of the allowed group and vice versa
       if we move the row to the not allowed lower area, it is moved to the bottom of the allowed group
       also prevents moves to the last row of a group (which is reserved for the add-item placeholder).
      */
      let rowInSourceSection = (sourceIndexPath.section > proposedDestinationIndexPath.section) ? 0 : numberOfItems - 1;

      return IndexPath(row: rowInSourceSection, section: sourceIndexPath.section)
    }
    // Prevents moves to the last row of a group (which is reserved for the add-item placeholder).
    else if (proposedDestinationIndexPath.row >= numberOfItems) {

      return IndexPath(row: numberOfItems - 1, section: sourceIndexPath.section)
    }
    // Passing all restrictions
    return proposedDestinationIndexPath
  }