代码之家  ›  专栏  ›  技术社区  ›  user3628240

Swift-tableView中的可移动行仅在一个节内,而不是在节之间

  •  1
  • user3628240  · 技术社区  · 6 年前

    这个 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)
    }
    
    2 回复  |  直到 6 年前
        1
  •  15
  •   Paulw11    6 年前

    你需要实施 UITableViewDelegate targetIndexPathForMoveFromRowAt .

    你的策略是允许移动如果源和目标 section

    这将限制移动到源节。

    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
    }
    
        2
  •  1
  •   ozmpai    5 年前

    通过实现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
      }