ios - 防止 moveRowAtIndexPath 超出固定索引

标签 ios xcode swift uitableview

我有一个 UITableViewController 显示动态表格数据,最后一行(使用单独的单元格原型(prototype))包含一个“添加”按钮。

我希望用户能够对表格行重新排序,但阻止他们将行移动到最后一行之后的点。

我已经实现了:

override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {

    return indexPath.row != elements.count

}

防止用户移动“添加”表行

我也在测试

    if(toIndexPath.row < library.regularTasks.count)

内部

override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)

这可以防止行在模型中被交换。

但是我看不到一种方法可以防止行被拖到 UI 中的最后一行

最佳答案

找到了!

我用过:

override func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath {  
// Test whether destination is allowed    
if(proposedDestinationIndexPath.row < elements.count){
      return proposedDestinationIndexPath // if allowed move to proposed destination
    }else{
        return sourceIndexPath // if not, send back to where it came from!
    } 
}

关于ios - 防止 moveRowAtIndexPath 超出固定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31517836/

相关文章:

ios - 毒蛇架构 : submodules in a module

iOS 谁是 subview 的文件所有者?

iphone - 使用 XCode for iPhone 实现多线程

iOS 今日扩展与核心数据

ios - dequeueReusableCellWithIdentifier 快速加载需要时间

ios - UIScreen 上的 snapshotViewAfterScreenUpdates 在设备上提供空白快照

iphone - 从 init 方法调用方法?

ios - TableViewController 中的多个自定义单元格

ios - Tableview Xib Shadow Swift

ios - swift:细胞中的图像