JPC*_*JPC 2 cocoa objective-c nstableview nstableviewcell
我一直在研究在表格中设置动画行NSTableView的moveRowAtIndex:toIndex方法.根据我的判断,排序并没有多大帮助.我对它是如何工作的解释是,如果我想将第0行移动到第4行,那么它们之间的行将被适当地处理.但是,如果我有一个带有支持它的数组的表视图,然后我对数组进行排序,我希望表视图从旧状态动画到新状态.我不知道哪些项目是移动的项目与那些移动以适应移动项目的项目.
例:
[A,B,C,D] --> [B,C,D,A]
Run Code Online (Sandbox Code Playgroud)
我知道第0行移到了第3行,所以我想说[tableView moveRowAtIndex:0 toIndex:3].但是如果我对[A,B,C,D]应用一些自定义排序操作使其看起来像[B,C,D,A],我实际上并不知道第0行移动到第3行而不是第1行,2和3移动到行0,1和2.我认为我应该能够指定所有的移动(第0行移动到第4行,第1行移动到第0行等)但是当我尝试时,动画看起来不正确.
有一个更好的方法吗?
编辑:我发现这个网站,似乎做我想要的,但似乎有点多的东西应该是简单的(至少我认为它应该是简单的)
小智 5
moveRowAtIndex:toIndex的文档说:"更改会在发送到表格时逐步发生".
从ABCDE到ECDAB的转换可以最好地说明"递增"的意义.
如果您只考虑初始索引和最终索引,它看起来像:
E: 4->0
C: 2->1
D: 3->2
A: 0->3
B: 1->4
Run Code Online (Sandbox Code Playgroud)
但是,在逐步执行更改时,"初始"索引可以在转换数组时跳转:
E: 4->0 (array is now EABCD)
C: 3->1 (array is now ECABD)
D: 4->2 (array is now ECDAB)
A: 3->3 (array unchanged)
B: 4->4 (array unchanged)
Run Code Online (Sandbox Code Playgroud)
基本上,您需要逐步告知NSTableView,需要移动哪些行才能到达与排序数组相同的数组.
这是一个非常简单的实现,它采用任意排序的数组并"重放"将原始数组转换为排序数组所需的移动:
// 'backing' is an NSMutableArray used by your data-source
NSArray* sorted = [backing sortedHowYouIntend];
[sorted enumerateObjectsUsingBlock:^(id obj, NSUInteger insertionPoint, BOOL *stop) {
NSUInteger deletionPoint = [backing indexOfObject:obj];
// Don't bother if there's no actual move taking place
if (insertionPoint == deletionPoint) return;
// 'replay' this particular move on our backing array
[backing removeObjectAtIndex:deletionPoint];
[backing insertObject:obj atIndex:insertionPoint];
// Now we tell the tableview to move the row
[tableView moveRowAtIndex:deletionPoint toIndex:insertionPoint];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
804 次 |
| 最近记录: |