阻止在TableView的特定单元格中重新排序

时间:2013-02-27 17:15:24

标签: ios xcode uitableview tableview

我想阻止要重新排序的单元格。

例如:

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

这可能吗?

我试图使用:

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

但是没有用。

感谢。

3 个答案:

答案 0 :(得分:9)

我找到了答案!

您可以使用UITableViewDelegate中的方法 targetIndexPathForMoveFromRowAtIndexPath

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

}

答案 1 :(得分:1)

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

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

UITableViewDataSource documentation

答案 2 :(得分:1)

SWIFT 5代码:

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

    if proposedDestinationIndexPath.row == 0 {
        return sourceIndexPath
    }
    
    return proposedDestinationIndexPath
}
相关问题