簡體   English   中英

Swift-tableView中的可移動行僅在一個區域內,而不在兩個區域之間

[英]Swift - Movable rows in tableView only within a section, not between

有沒有一種方法可以防止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)
}

您將需要實現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
}

您可以通過實現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
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM