【问题标题】:Block reordering in a specific cell of a TableView在 TableView 的特定单元格中阻止重新排序
【发布时间】:2013-02-13 14:48:52
【问题描述】:

我想阻止一个单元格重新排序。

例如:

我有一个有 4 行的表格视图,但不希望第一个单元格可以重新排序。

这可能吗?

我试过用:

if(indexPath.row == 0)
{
    [cell setEditing:NO animated:NO];
}

但不起作用。

谢谢。

【问题讨论】:

    标签: ios xcode uitableview tableview


    【解决方案1】:

    SWIFT 5 代码:

    func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
    
        if proposedDestinationIndexPath.row == 0 {
            return sourceIndexPath
        }
        
        return proposedDestinationIndexPath
    }
    

    【讨论】:

      【解决方案2】:

      我找到了答案!

      您可以使用 UITableViewDelegate 中的 targetIndexPathForMoveFromRowAtIndexPath 方法。

      -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
          if (proposedDestinationIndexPath.row == 0) {
              return sourceIndexPath;
          }
          return proposedDestinationIndexPath;
      

      }

      【讨论】:

        【解决方案3】:

        您的 UITableViewDataSource 应该实现 -(BOOL)tableView:canMoveRowAtIndexPath: 并为 indexPath.row == 0 返回 NO

        - (BOOL)tableView:canMoveRowAtIndexPath:(NSIndexPath *)indexPath
        {
            return (indexPath.row != 0);
        }
        

        UITableViewDataSource documentation

        【讨论】:

        • 该单元格无法重新排序,但是当您单击 EDIT BUTTON 重新排序其他单元格时,它也会更改位置...我希望它是静态的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        相关资源
        最近更新 更多