gpt4 book ai didi

ios - 防止 moveRowAtIndexPath 超出固定索引

转载 作者:行者123 更新时间:2023-11-28 13:10:30 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com