在IndexPath中移动Row时更新数据源的问题 [英] issue with updating the datasource while moving Row at IndexPath

查看:66
本文介绍了在IndexPath中移动Row时更新数据源的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过滑动手势将行从一个位置移动到另一个位置.当我向右滑动任何一个单元格时,被滑动的单元格应该移至该单元格的底部,为此,我已经编写了代码,并且在某些情况下它可以正常工作,即假设我在索引位置0处滑动了该单元格,它可以正确地转到单元格的底部,假设我在数组中有"A,B,C" ,因此表显示为"A,B,C".现在假设我选择"A" 滑动到底部的 0 位置,现在表格将显示 B,C,A ,这是正确的.现在,我滑过位于位置 1 "C" ,以便将其移至底部.现在我的表格显示 C,A,B.但实际上它应该显示 B,A,C.

I am trying to move a row from one position to another on swipe gestures, ie. when I swipe right any of the cell, the swiped cell should go to the bottom of the cell, for this I have written the code, and it works fine in some condition i.e suppose I swiped the cell at index position 0 it goes correctly to the bottom of the cell, suppose I have "A, B, C" in my array, so the table displays "A, B, C" .Now suppose I select " A" to swipe which is at position 0 it goes at bottom and and now the table will display B,C,A which is correct. Now I swipe "C" which is at postion 1 so that it should go to bottom. now my table displays C,A,B. But infact it should have displayed B,A,C.

下面是我的代码

if (state == JTTableViewCellEditingStateRight) 
{

   NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

  [tableView moveRowAtIndexPath:selectedIndexPath toIndexPath:[NSIndexPath indexPathForRow:[self.rows count]-numberOfMoves inSection:0]];  
   [self moveRows];
} 


- (void)moveRows
{

    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    NSString *selectedString = [self.rows objectAtIndex:selectedIndexPath.row];     

    [self.rows removeObjectAtIndex:selectedIndexPath.row];
    [self.rows insertObject:selectedString atIndex:[self.rows count]];   
}

问候 兰吉特

推荐答案

让我们分析您的代码:

[self.rows insertObject:selectedString atIndex:[self.rows count]]; 

似乎您将新项始终放在数组的末尾而不是新位置.

it seems you put the new item always to the end of the array and not to the new position.

移动对象的正确方法是使用数据源:

the proper way to move the objects is your data source is the following:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    id _object = [self.rows objectAtIndex:fromIndexPath.row];
    [self.rows removeObjectAtIndex:fromIndexPath.row];
    [self.rows insertObject:_object atIndex:toIndexPath.row];
}

这可能会对您有所帮助.

it might helps you.

这篇关于在IndexPath中移动Row时更新数据源的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆