如何防止将 UITableViewCell 移动到特定索引

Pau*_*ulG 0 uitableview ios

在我的应用程序中,用户可以UITableViewCell使用编辑按钮和拖放来移动行。

我不希望用户能够将单元格移动到第 0 行。如果行为 0,我已经在我的NSMutableArray地方工作,然后不要重新排列集合中的对象。但即使这样,可见表格仍然显示第 0 行的单元格。

如何以图形方式防止这种情况?

我尝试了以下方法:

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

        return proposedDestinationIndexPath;
}
Run Code Online (Sandbox Code Playgroud)

但是EXC_BAD_ACCESS当我尝试将第 1 行的单元格移动到第 0 行时,它会因错误而崩溃。

Mic*_*uba 5

你的方法是正确的。问题是tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath方法应该返回NSIndexPath*,但你返回一个整数。修复很简单:

if(proposedDestinationIndexPath.row == 0)
{
    return [NSIndexPath indexPathForRow:1 inSection: proposedDestinationIndexPath.section];
}
Run Code Online (Sandbox Code Playgroud)