简体   繁体   English

委托方法'canMoveRowAtIndexPath'的目的是什么?

[英]What is the purpose of the delegate method 'canMoveRowAtIndexPath'?

I'm working on a UI component right now, and as it behaves similarly to UITableView, I'm heavily modeling the delegate and data source protocols after those of UITableView. 我现在正在开发一个UI组件,因为它的行为类似于UITableView,我在UITableView之后对代理和数据源协议进行了大量建模。 However, I noticed one method that I don't quite understand- 'canMoveRowAtIndexPath'. 但是,我注意到一种我不太了解的方法 - 'canMoveRowAtIndexPath'。

This essentially allows the delegate to specify whether it wants the given cell to be 'movable'. 这基本上允许委托者指定它是否希望给定的单元格是“可移动的”。 However, wouldn't dropping another movable cell into a higher index than the immovable cell (ie 'above' it in the table) cause it to indirectly move anyway? 但是,不会将另一个可移动单元放入比不可移动单元更高的索引(即表中的'上方')导致它无论如何间接移动? (since every cell below the moved one would be pushed down one row). (因为移动的下面的每个单元格都会向下推一行)。

So basically, my question is what is the point of this method? 所以基本上,我的问题是这个方法的重点是什么? Can anyone provide an example use-case for it? 任何人都可以提供一个示例用例吗? Because I'm debating whether I should bother including it in my component or not. 因为我在讨论是否应该在我的组件中包含它。

If anything, I would think perhaps a more useful delegate method would be something such as 'canMoveRowInSection', which would allow you to specify whether any rows in a given section can be moved. 如果有的话,我想也许一个更有用的委托方法可能是'canMoveRowInSection',它允许你指定是否可以移动给定部分中的任何行。 That would then allow you to disable reordering of a particular section, and moving other rows outside of that section would not affect the ordering of the rows inside it. 然后,这将允许您禁用特定部分的重新排序,并且移动该部分之外的其他行不会影响其中的行的排序。

I know Apple engineers provided this method for a reason, I just can't see what that reason might be. 我知道Apple工程师出于某种原因提供了这种方法,我只是看不出那个原因。

Thanks for any insight! 感谢您的任何见解!

canMoveRowAtIndexPath tells the UITableView that if the table is in editing mode, the cells (or cell, if you choose specifically) can be moved up and down. canMoveRowAtIndexPath告诉UITableView如果表处于编辑模式,则可以上下移动单元格(或单元格,如果您具体选择)。

It's up to you the developer to handle the other side of that move. 这取决于开发人员处理该移动的另一面。

For instance, say you have an array ( NSMutableArray to be exact) of "A", "B", "C" and you want to rearrange that array to be "B", "C", "A". 例如,假设您有一个“A”,“B”,“C”的数组(确切地说NSMutableArray ,并且您想要将该数组重新排列为“B”,“C”,“A”。 You need to make that change in the array based on the location of the cell being moved and save that array. 您需要根据要移动的单元格的位置在数组中进行更改并保存该数组。

Example

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    id objectToMove = [[array objectAtIndex:fromIndexPath.row] retain];
    [array removeObjectAtIndex:fromIndexPath.row];
    [array insertObject:objectToMove atIndex:toIndexPath.row];
    [objectToMove release];
}

Section Example 部分示例

This example says that if the table section is 0, then no cells can move. 此示例表示如果表部分为0,则没有单元格可以移动。 Any other section (say you have 3), those cells in section 1 and 2 CAN move. 任何其他部分(比如说你有3个),第1部分和第2部分中的那些单元格都会移动。 You will still need to handle the array accordingly. 您仍然需要相应地处理数组。

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ( indexPath.section == 0 )
        return NO;

    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 `contextWithCGContext:options:` 方法的目的是什么? - What is the purpose of `contextWithCGContext:options:` method? NSInvocation类上的setSelector方法的目的是什么? - What's the purpose of the setSelector method on the NSInvocation class? EAAccessoryDe​​legate中的– annexDidDisconnect:方法的目的是什么? - What is the purpose of the – accessoryDidDisconnect: method within the EAAccessoryDelegate? 我的UIAlertView委托方法有什么问题? - What is wrong with my UIAlertView delegate method? 触摸MKPinAnnotationView时调用的委托方法是什么? - What is the delegate method that is called when an MKPinAnnotationView is touched? 后续委托方法调用之间有什么区别? - What is the difference between following delegate method call? 故意调用CLLocationManager委托的didFailWithError: - call didFailWithError: of CLLocationManager delegate on purpose iPhone中的类方法和委托方法有什么区别 - what is the different between class method and delegate method in iPhone NSArray class 的“sortedArrayHint”方法,这个方法的目的是什么,它是如何使用的? - “sortedArrayHint” method of NSArray class, what is the purpose of this method and how is it used? 永远不会调用CanMoveRowAtIndexPath - CanMoveRowAtIndexPath is never called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM