简体   繁体   English

在TableView中对单元格重新排序时获取索引路径?

[英]getting indexpath while reordering cells in tableview?

I want change the cells order in my tableview 我想更改表格视图中的单元格顺序

I am using following code 我正在使用以下代码

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath  
{    
    [tableView moveRowAtIndexPath:path1 toIndexPath:path2];
}

here path1 indexpath of row which user selected now I want find the indexpath of the row the user drop that cell so how can I find that toIndexPath(path2). 这里是用户现在选择的行的path1索引路径,我想找到用户删除该单元格的行的索引路径,以便如何找到toIndexPath(path2)。

I think you're confused with the purpose of this method - this is a callback function of the UITableViewDataSource that's designed to notify the data model of changes in the table. 我认为您对该方法的目的感到困惑-这是UITableViewDataSource的回调函数,旨在将表中的更改通知数据模型。 From the docs: 从文档:

Tells the data source to move a row at a specific location in the table view to another location. 告诉数据源将表视图中特定位置的行移动到另一个位置。

The UITableView object sends this message to the data source when the user presses the reorder control in fromRow. 当用户在fromRow中按下重新排序控件时,UITableView对象会将此消息发送到数据源。

This means that what you need to do here is to make sure your model (Usually an array) is rearranged to sync properly with these changes. 这意味着您在这里需要做的是确保模型(通常是数组)已重新排列以与这些更改正确同步。

Hope this gives you a good direction. 希望这会给您一个好的方向。

You should implement: 您应该实现:

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

& also: 以及:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 //do whatever you want after the row has been moved
}

Here is an example of drag N drop: 这是拖动N下降的示例:

https://github.com/coderDove/UITableView-Drag-n-Drop https://github.com/coderDove/UITableView-Drag-n-Drop

Hope this Helps!!! 希望这可以帮助!!!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM