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

标签 ios uitableview

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

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

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

我试过以下方法:

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

        return proposedDestinationIndexPath;
}

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

最佳答案

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

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

关于ios - 如何防止将 UITableViewCell 移动到特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362913/

相关文章:

android - 在 React Native 中添加分享操作

ios - 如何使用 NSNumbers 在 switch 语句中设置 NSDate

ios - 如何使用 Json 数组到 UITableview

iphone - 如何使用用户的 iPod 库艺术家索引 UITableView?

ios - UITableViewController 中的 UITableView 行分隔符关闭了吗?

ios - byteArray to Hex NSString - 添加了一些错误的十六进制内容

ios - 带有 URL Scheme 的谷歌地图?

ios - iOS 启动图像可以是 GIF 动画吗?

ios - 快速调整标签栏大小后调整 subview 大小

ios - 在 Collection View 中显示两个不同的单元格 - Swift 2.0 iOS