在IndexPath上移动行时更新数据源

我试图从一个位置移动到另一个滑动手势,即。 当我向右滑动任何一个单元格时,滑过的单元格应该到单元格的底部,为此,我已经编写了代码,并且在某种情况下工作正常,即假设我在索引位置0处滑动单元格,单元格的底部,假设我的数组中有“A,B,C” ,所以表格显示“A,B,C”。现在假设我select了“A”现在表格会显示B,C,A ,这是正确的。 现在我滑动位置1的 “C” ,以便它应该到底。 现在我的表格显示C,A,B。 但事实上它应该显示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]]; } 

关心Ranjit

让我们分析一下你的代码:

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

看起来你把新的项目总是到arrays的末尾,而不是新的位置。


移动对象的正确方法是您的数据源如下:

 - (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]; } 

它可能会帮助你。