8

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

例如:

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

那可能吗?

我试过使用:

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

但不起作用。

谢谢。

4

3 回答 3

11

我找到了答案!

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

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

}

于 2013-02-27T18:26:09.197 回答
1

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

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

UITableViewDataSource 文档

于 2013-02-27T17:21:18.217 回答
1

SWIFT 5 代码:

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

    if proposedDestinationIndexPath.row == 0 {
        return sourceIndexPath
    }
    
    return proposedDestinationIndexPath
}
于 2020-08-12T09:24:14.977 回答